12 ms C solution , is it right?


  • 0
    bool containsDuplicate(int* nums, int numsSize) {
    
        int i,j;
        int key;
        
        for(i=1; i<numsSize; i++){
            j = i-1;
            key = nums[i];
            while (j>=0 && nums[j]>key){
                nums[j+1] = nums[j];
                j--;
            }
            if(j>=0 && nums[j] == key) return true;
            nums[j+1] = key;
        }
    
        // for(i=1; i<numsSize; i++)  if(nums[i-1] == nums[i]) return true;
        return false;
    }

  • 0

    I don't know why it still work without the second For Loop.


  • 0

    @wuhan326 Actually you've already finished that comparing while doing your insertSort.

    while (j>0 && nums[j]>key) 
    if(nums[j] == key) return true;
    

Log in to reply
 

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