my java solution


  • 0
    U
    public class Solution {
        public List<Integer> findDuplicates(int[] nums) {
            int i=0;
            while(i<nums.length){
                int temp=nums[i];
                if(temp<0||nums[temp-1]<0){
                    i++;
                    continue;
                }
                nums[i]=nums[temp-1];
                nums[temp-1]=-1;
            }
            List<Integer> res = new ArrayList<Integer>();
            for(i=0;i<nums.length;i++){
                if(nums[i]>0){
                    res.add(nums[i]);
                }
            }
            return res;
            
        }
    }
    

Log in to reply
 

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