2ms..(I think it couldn't be that fast..) solution in java


  • 1
    Z
    public boolean containsNearbyDuplicate(int[] nums, int k) {
            int[][] table = new int[3][nums.length];
            
            for(int i = 0; i < nums.length; i++)
            {
                int num = nums[i];
                int place = num % nums.length;
                if (place < 0) place += nums.length;
                
                while (table[0][place] != 0)
                {
                    if (table[1][place] == num && table[2][place] + k >= i)
                        return true;
                    else if (table[1][place] == num)
                        break;
                    else
                        place = (place + 1) % nums.length;
                }
                
                table[0][place] = 1;
                table[1][place] = num;
                table[2][place] = i;
            }
            return false;
        }

Log in to reply
 

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