My 2ms Java O(n) solution O(1) space


  • 0
    R
    public class Solution {
        public int findDuplicate(int[] nums) {
            
            for(int i = 0; i<nums.length; i++){
                int temp = Math.abs(nums[i]);
                if(nums[temp-1] < 0){
                    return temp;
                }else{
                    nums[temp-1] = -nums[temp-1];
                }
            }
            return -1;
            
            
        }
    }
    

  • 0

    but the array is read only and u can't modify it.


Log in to reply
 

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