Java solution, anyone can tell me the space complexity?


  • 0
    T
    public int findDuplicate(int[] nums) {
        for (int i = 0; i< nums.length; i++){
             int value = nums[i];
             int start = 0;
             int end = nums.length-1;
             while (start < end){
               if(((i != start) && (nums[start] == value)) || ((i != end) && (nums[end] == value))){
                   return value;
               } else {
                   start++;
                   end --;
               }
           }
        }
        return -1;
    }

  • 0
    A

    Your space complexity is constant, O(1).


Log in to reply
 

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