当前位置:网站首页>2021-11-10:o (1) time inserts, deletes and obtains random elements. Implement ra
2021-11-10:o (1) time inserts, deletes and obtains random elements. Implement ra
2022-06-24 01:52:00 【Fuda scaffold constructor's daily question】
2021-11-10:O(1) Time insertion 、 Delete and get random elements . Realization RandomizedSet class :RandomizedSet() initialization RandomizedSet object .bool insert(int val) When element val When there is no , Insert the item into the collection , And back to true ; otherwise , return false .bool remove(int val) When element val In existence , Remove the item from the collection , And back to true ; otherwise , return false .int getRandom() Random return of an item in an existing collection ( The test case guarantees that there is at least one element in the collection when this method is called ). Every element should have Same probability Returned . You must implement all the functions of the class , And satisfy the of each function Average The time complexity is O(1) . Power button 380.
answer 2021-11-10:
Two hash tables .
v→index.
index→v.
index It must be continuous .
Delete the element in the middle , Replace... With the last element . such index It's continuous .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
nrs := NewRandomizedSet()
nrs.insert(6)
nrs.insert(3)
nrs.insert(7)
nrs.insert(9)
nrs.insert(8)
fmt.Println(nrs.getRandom())
}
type RandomizedSet struct {
keyIndexMap map[int]int
indexKeyMap map[int]int
size int
}
func NewRandomizedSet() *RandomizedSet {
res := &RandomizedSet{}
res.keyIndexMap = make(map[int]int)
res.indexKeyMap = make(map[int]int)
res.size = 0
return res
}
func (this *RandomizedSet) insert(val int) bool {
if _, ok := this.keyIndexMap[val]; !ok {
this.keyIndexMap[val] = this.size
this.indexKeyMap[this.size] = val
this.size++
return true
}
return false
}
func (this *RandomizedSet) remove(val int) bool {
if _, ok := this.keyIndexMap[val]; ok {
deleteIndex := this.keyIndexMap[val]
this.size--
lastIndex := this.size
lastKey := this.indexKeyMap[lastIndex]
this.keyIndexMap[lastKey] = deleteIndex
this.indexKeyMap[deleteIndex] = lastKey
delete(this.keyIndexMap, val)
delete(this.indexKeyMap, lastIndex)
return true
}
return false
}
func (this *RandomizedSet) getRandom() int {
if this.size == 0 {
return -1
}
randomIndex := rand.Intn(this.size)
return this.indexKeyMap[randomIndex]
}The results are as follows :
边栏推荐
- Custom form dynamic form form designer process engine design scheme
- Practical case - Tencent security hosting service MSS helped "zero accident" during the period of digital Guangdong re insurance!
- Based on ARM embedded real-time streaming media service development and deployment, easygbs supports arm64 architecture
- Software cost evaluation: a method for estimating software scale by fast function point method
- NFS file systems - mount and optimize
- NFS operations and deployment
- Netease Shufan: Data productivity platform 2.0 promotes "everyone uses data and always uses data"
- Tencent music, slow down?
- Grpc: implement grpc proxy
- Why promote steam education?
猜你喜欢

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
随机推荐
Gin framework: implementing service end flow limiting Middleware
Use Navicat software to connect self built database (Linux system)
Moment. JS to UTC format
Analysis report on operation situation and development trend of global and Chinese diisobutyl aluminum hydride (Dibah) industry 2022-2028
Six steps from strategy to product - johncutlefish
Go language core 36 lectures (go language practice and application VII) -- learning notes
[technology for grass planting] lightweight 248 helps individual developers go to the cloud
[tcapulusdb knowledge base] how to get started with tcapulus SQL driver?
What are the categories of code signing certificates? What are the differences between different types of certificates?
Introduction to trusted service manager
[tcapulusdb knowledge base] tcapulusdb introduction Questions Summary
DCOM horizontal movement of Intranet penetration
SAP WM displays the standard report lx09 of TR item
November 15, 2021: add four numbers II. Here are four integer arrays nums1, num
Thorough and thorough analysis of factory method mode
How to restart the server through the fortress machine how to log in to the fortress machine
tokio_ Rustls self signed certificate
How does SAP retail view which Po the allocation table is created with reference to?
Global and Chinese gallium industry market panoramic survey and investment strategy proposal report 2022-2028
Login server in VNC mode