Code accepted when should have failed


  • 0
    B
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        for (int i=0;i<nums.length;i++)
            for (int j=i+1; j<=i+k;j++) {
                if (j>=nums.length) return false;
                if (nums[i] == nums[j]) return true;
            }
        return false;
    }
    

    Test case: {1,2,3,4,5,6,7,8,9,9}, 3

    returns false but should be true. Was accepted anyways.


  • 0
    W

    when j >=nums.length ,u cant return false, should break the loop of j ,go next i


  • 0

    Thanks, I have just added this test case.


  • 0
    L

    the poster was saying his code was AC while he knew his code was wrong :)


Log in to reply
 

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