leetcode --Contains Duplicate II

来源:互联网 发布:ad线路板画图软件 编辑:程序博客网 时间:2024/06/06 07:25

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jis at most k.

注:特殊情况k=0时直接返回false

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

0 0
原创粉丝点击