原文链接: https://leetcode-cn.com/problems/insert-delete-getrandom-o1 英文原文Implement the RandomizedSet class: RandomizedSet() Initializes the RandomizedSet object. bool insert(int val) Inserts an item val into the set if not present. Returns true if the i…
标签详情
随机化 分类
原文链接: https://leetcode-cn.com/problems/insert-delete-getrandom-o1-duplicates-allowed 英文原文RandomizedCollection is a data structure that contains a collection of numbers, possibly duplicates (i.e., a multiset). It should support inserting and removing …
原文链接: https://leetcode-cn.com/problems/linked-list-random-node 英文原文Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Implement the Solution class: Solution(ListNod…
原文链接: https://leetcode-cn.com/problems/shuffle-an-array 英文原文Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling. Implement the Solution cl…
原文链接: https://leetcode-cn.com/problems/random-pick-index 英文原文Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Implement the …
原文链接: https://leetcode-cn.com/problems/random-pick-with-blacklist 英文原文You are given an integer n and an array of unique integers blacklist. Design an algorithm to pick a random integer in the range [0, n - 1] that is not in blacklist. Any integer tha…
原文链接: https://leetcode-cn.com/problems/random-pick-with-weight 英文原文You are given a 0-indexed array of positive integers w where w[i] describes the weight of the ith index. You need to implement the function pickIndex(), which randomly picks an index …
原文链接: https://leetcode-cn.com/problems/random-flip-matrix 英文原文There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i…
原文链接: https://leetcode-cn.com/problems/implement-rand10-using-rand7 英文原文Given the API rand7() that generates a uniform random integer in the range [1, 7], write a function rand10() that generates a uniform random integer in the range [1, 10]. You can…
原文链接: https://leetcode-cn.com/problems/random-point-in-non-overlapping-rectangles 英文原文You are given an array of non-overlapping axis-aligned rectangles rects where rects[i] = [ai, bi, xi, yi] indicates that (ai, bi) is the bottom-left corner point of…
原文链接: https://leetcode-cn.com/problems/generate-random-point-in-a-circle 英文原文Given the radius and the position of the center of a circle, implement the function randPoint which generates a uniform random point inside the circle. Implement the Solutio…
原文链接: https://leetcode-cn.com/problems/best-position-for-a-service-centre 英文原文A delivery company wants to build a new service centre in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the n…
原文链接: https://leetcode-cn.com/problems/FortPu 中文题目设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构: insert(val):当元素 val 不存在时返回 true ,并向集合中插入该项,否则返回 false 。 remove(val):当元素 val 存在时返回 true ,并从集合中移除该项,否则返回 false 。 getRandom:随机返回现有集合中的一项。每个元素应该有 相同的概率 被返回。 示例 : 输入: in…
原文链接: https://leetcode-cn.com/problems/cuyjEf 中文题目给定一个正整数数组 w ,其中 w[i] 代表下标 i 的权重(下标从 0 开始),请写一个函数 pickIndex ,它可以随机地获取下标 i,选取下标 i 的概率与 w[i] 成正比。 例如,对于 w = [1, 3],挑选下标 0 的概率为 1 / (1 + 3) = 0.25 (即,25%),而选取下标 1 的概率为 3 / (1 + 3) = 0.75(即,75%)。 也就是说,选取下标…