Java simple solution using rotate replacing


  • 0
    C
    public class Solution {
        public List<Integer> findDisappearedNumbers(int[] nums) {
            for(int i = 0;i < nums.length;i++){
                if(nums[i] != i + 1){
                    for(int j = nums[i] - 1,cur = 0; nums[j] != j + 1; ){
                        cur = nums[j];
                        nums[j] = j + 1;
                        j = cur - 1;
                    }
                }
            }
            List<Integer> result = new ArrayList<Integer>();
            for(int i = 0;i < nums.length;i++){
                if(nums[i] != i + 1){
                    result.add(i + 1);
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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