GeezerVison is showing github.com — New Problem Solution "Random Pick Index" · weimo/leetcode@51a0624
File tree
Expand file treeCollapse file tree
Open diff view settings
algorithms/cpp/randomPickIndex
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
8 | 8 |
|
9 | 9 | | # | Title | Solution | Difficulty | |
10 | 10 | |---| ----- | -------- | ---------- | |
| 11 | +|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/) | [C++](./algorithms/cpp/randomPickIndex/RandomPickIndex.cpp)|Medium| |
11 | 12 | |397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/) | [C++](./algorithms/cpp/integerReplacement/IntegerReplacement.cpp)|Medium| |
12 | 13 | |396|[Rotate Function](https://leetcode.com/problems/rotate-function/) | [C++](./algorithms/cpp/rotateFunction/RotateFunction.cpp)|Easy| |
13 | 14 | |395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [C++](./algorithms/cpp/longestSubstringWithAtLeastKRepeatingCharacters/LongestSubstringWithAtLeastKRepeatingCharacters.cpp)|Medium| |
|
Collapse file
Copy file name to clipboard +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// Source : https://leetcode.com/problems/random-pick-index/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2016-11-04 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Given an array of integers with possible duplicates, randomly output the index of a |
| 8 | + * given target number. You can assume that the given target number must exist in the |
| 9 | + * array. |
| 10 | + * |
| 11 | + * Note: |
| 12 | + * The array size can be very large. Solution that uses too much extra space will not |
| 13 | + * pass the judge. |
| 14 | + * |
| 15 | + * Example: |
| 16 | + * |
| 17 | + * int[] nums = new int[] {1,2,3,3,3}; |
| 18 | + * Solution solution = new Solution(nums); |
| 19 | + * |
| 20 | + * // pick(3) should return either index 2, 3, or 4 randomly. Each index should have |
| 21 | + * equal probability of returning. |
| 22 | + * solution.pick(3); |
| 23 | + * |
| 24 | + * // pick(1) should return 0. Since in the array only nums[0] is equal to 1. |
| 25 | + * solution.pick(1); |
| 26 | + ***************************************************************************************/ |
| 27 | + |
| 28 | +class Solution { |
| 29 | +private: |
| 30 | + vector<int> nums; |
| 31 | +public: |
| 32 | + Solution(vector<int> nums) { |
| 33 | + srand(time(0)); |
| 34 | + this->nums = nums; |
| 35 | + } |
| 36 | + |
| 37 | + int pick(int target) { |
| 38 | + // we just randomly pick a number from the array, |
| 39 | + // if the number is target just return the index. |
| 40 | + // otherwise, keep picking the number randomly. |
| 41 | + while(true) { |
| 42 | + int idx = rand() % nums.size(); |
| 43 | + if ( target == nums[idx] ) { |
| 44 | + return idx; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Your Solution object will be instantiated and called as such: |
| 52 | + * Solution obj = new Solution(nums); |
| 53 | + * int param_1 = obj.pick(target); |
| 54 | + */ |