Leetcode - Random Pick Index
My code:
public class Solution {
int[] nums;
int result = Integer.MAX_VALUE;
Random r;
public Solution(int[] nums) {
this.nums = nums;
r = new Random();
}
public int pick(int target) {
int c = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
c++;
if (r.nextInt(c) == 0) {
result = i;
}
}
}
return result;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
差不多的做法。
Anyway, Good luck, Richardo! -- 10/12/2016
相關文章
- LeetCode-Random Pick IndexLeetCoderandomIndex
- LeetCode 398 Random Pick Index(蓄水池抽樣典型例題)LeetCoderandomIndex
- [LeetCode] 528. Random Pick with WeightLeetCoderandom
- Leetcode Copy List with Random PointerLeetCoderandom
- Leetcode-Copy List with Random PointerLeetCoderandom
- Copy List with Random Pointer leetcode javarandomLeetCodeJava
- LeetCode 138. Copy List with Random PointerLeetCoderandom
- LeetCode 382 Linked List Random NodeLeetCoderandom
- 【Leetcode】138. Copy List with Random PointerLeetCoderandom
- LeetCode-Linked List Random NodeLeetCoderandom
- LeetCode138:Copy List with Random PointerLeetCoderandom
- leetcode138: Copy List with Random PointerLeetCoderandom
- LeetCode-H index IILeetCodeIndex
- [LeetCode] 724. Find Pivot IndexLeetCodeIndex
- [LeetCode] 274. H-IndexLeetCodeIndex
- [LeetCode] 852. Peak Index in a Mountain ArrayLeetCodeIndexAI
- git cherry-pickGit
- [LeetCode] 2903. Find Indices With Index and Value Difference ILeetCodeIndex
- random 模組random
- pick靶場-sql注入SQL
- git cherry-pick 教程Git
- Random和Math.random()簡單總結random
- [LeetCode] H-Index II 求H指數之二LeetCodeIndex
- 有關git cherry-pickGit
- Math.random()random
- Math.randomrandom
- random()函式random函式
- dbms_randomrandom
- LeetCode 138:複製帶隨機指標的連結串列 Copy List with Random PointerLeetCode隨機指標random
- 【DBMS_RANDOM】使用 DBMS_RANDOM包生成隨機字串random隨機字串
- Pick和Extract的區別示例
- python random模組Pythonrandom
- np.random.choicerandom
- DBMS_RANDOM使用random
- 測試random類random
- 11 random案例1random
- Random 類的使用random
- different random numbers generatorrandom