Please help! Is my solution correct? (Accepted by OJ though!)

  • 0

    Is my solution correct? Could anyone explain why if correct or wrong?
    Non-deterministic calls!

    public class Solution {
        int[] nums;
        public Solution(int[] nums) {
            this.nums = nums;
        public int pick(int target) {
            //Since selection of each index is supposed to be equally likely... at most in n random calls we will get an index that has target in it. Requires no space, O(N)!! 
            Random r = new Random();
            int n = nums.length;
            int val = r.nextInt(n);
            while (nums[val] != target) {
                val = r.nextInt(n);
            return val;
     * Your Solution object will be instantiated and called as such:
     * Solution obj = new Solution(nums);
     * int param_1 = obj.pick(target);

Log in to reply

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