randomized_set

  • 2022-12-14
  • 浏览 (336)

randomized_set.go 源码

package leetcode

import (
	"math/rand"
	"time"
)

type RandomizedSet struct {
	m map[int]int
	n []int
}

func constructor() RandomizedSet {
	var n []int
	return RandomizedSet{make(map[int]int), n}
}

func (this *RandomizedSet) Insert(val int) bool {
	if _, ok := this.m[val]; ok {
		return false
	}

	this.n = append(this.n, val)
	this.m[val] = len(this.n) - 1
	return true
}

func (this *RandomizedSet) Remove(val int) bool {
	if _, ok := this.m[val]; !ok {
		return false
	}

	n, idx := len(this.n), this.m[val]
	this.m[this.n[n-1]] = idx
	this.n[n-1], this.n[idx] = this.n[idx], this.n[n-1]

	delete(this.m, val)
	this.n = this.n[:n-1]
	return true
}

func (this *RandomizedSet) GetRandom() int {
	rand.Seed(time.Now().UnixNano())
	return this.n[rand.Intn(len(this.n))]
}

你可能感兴趣的文章

array_intersection

array_intersection_ii

find_all_anagrams

0  赞