Why is my solution incorrect? Any help would be appreciated. Thank you.


  • 0
    G
    class Solution {
    public:
    
    
        vector<int> Res;
    
        Solution(vector<int> nums) 
        :
        Res(nums.begin(),nums.end())
        {
            srand(time(NULL));
        }
        
        int pick(int target) 
        {
            int i = 0;
            int j = Res.size();
            int Idx = 0;
            
            while(i < j)
            {
                Idx = (rand() % (j - i));
                
                if(Res[Idx] == target)
                {
                    break;
                }
                
                swap(Res[Idx],Res[j - 1]);
                j--;
            }
            
            return (Idx);
        }
        
    };
    

  • 0
    Y

    Please delete the srand(time(NULL)),


Log in to reply
 

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