Java inline O(1) space +O(n) Simple Solution


  • 0
    H
    public class Solution {
        public List<Integer> findDuplicates(int[] nums) {
             List<Integer> ans = new ArrayList<>();
             for(int i=0;i<nums.length;i++){nums[(nums[i]-1)%nums.length] += nums.length;}
             for(int i=0;i<nums.length;i++){if(nums[i]>2*nums.length) ans.add(i+1);}
             return ans;
            
        }
    }
    

Log in to reply
 

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