public class Solution { int[] nums; Random rnd; public Solution(int[] nums) { this.nums = nums; this.rnd = new Random(); } public int pick(int target) { int result = -1; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != target) continue; if (rnd.nextInt(++count) == 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); */
https://leetcode.com/problems/random-pick-index/#/description