C# use hash and index beat 100%


  • 0
    Y

    public class Solution {
    public bool ContainsNearbyDuplicate(int[] nums, int k) {
    Dictionary<int, int> ind=new Dictionary<int,int>();
    for(int i=0;i<nums.Length;i++)
    {
    if(ind.ContainsKey(nums[i]))
    {
    if((i - ind[nums[i]]) <= k)
    return true;
    else
    ind[nums[i]] = i;
    }
    else
    {
    ind.Add(nums[i],i);
    }
    }
    return false;
    }
    }


Log in to reply
 

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