398.Random Pick Index

class Solution {
    int[] nums;
    Random random;
    public Solution(int[] nums) {
        this.nums = nums;
        random = new Random();
    }

    public int pick(int target) {
        int cntOfTarget = 0;
        int ans = -1;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == target){
                cntOfTarget++;
                if(random.nextInt(cntOfTarget) == 0){
                    ans = i;
                }
            }
        }
        return ans;
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */

results matching ""

    No results matching ""