首页 > 代码库 > 448. Find All Numbers Disappeared in an Array Add to List

448. Find All Numbers Disappeared in an Array Add to List

题目描述

技术分享

 

题目分析

有个[1,n]的条件要充分利用起来。

 

题目代码

public class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> list=new ArrayList<Integer>();
        if(nums==null || nums.length==0) return list;
        if(nums.length == 1 ){
            list.add(nums[0]);
            return list;
        }
        int i=0,temp=nums[0],t;
        while(true){
            if(temp>0){
                t=nums[temp-1];
                nums[temp-1]=0;
                temp=t;
            }
            else{
                i++;
                if(i>=nums.length) break;
                temp=nums[i];
            }
        }
        for(i=0;i<nums.length;i++){
            if(nums[i]!=0){
                list.add(i+1);
            }
        }
        return list;
    }
}

 

448. Find All Numbers Disappeared in an Array Add to List