Java, 1ms, only 9 lines. Is this correct?


  • -3
    B
    public class Solution {
    public int findDuplicate(int[] nums) {
        int[] seen = new int[nums.length];
        for(int i = 0; i < nums.length; i++)
            if(++seen[nums[i] - 1] > 1)
                return nums[i];
        throw new IllegalArgumentException("Invalid Input");
    }
    

    }


  • 0
    O

    nope it has to use constant additional memory


  • 0
    W

    You must use only constant, O(1) extra space.
    I think the seen have consume O(n) extra space.


Log in to reply
 

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