C implementation - request for comments


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

    23 test cases passed in 926ms.
    Any better way of doing?


Log in to reply
 

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