My n00b solution. Hoping to learn from others here


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

  • 0
    L

    this is absolutely time-costing, not a very good way. My thought is using hash table to quickly access to the number that has been visited.


Log in to reply
 

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