easy Java solution


  • 0
    S
    
        private int[] nums;
        
        public Solution(int[] nums) {
            this.nums = nums;
        }
        
        public int pick(int target) {
            Random r = new Random();
            boolean found = false;
            int count =0;
            while (!found){
                int index = r.nextInt(nums.length);
                if(nums[index] == target){
                    count = index;
                    found = true;
                    
                }
            }
            return count;
            }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.