Java Solution With Swap


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

    }


Log in to reply
 

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