1. 程式人生 > >leetcode 448 Find All Numbers Disappeared in an Array

leetcode 448 Find All Numbers Disappeared in an Array

integer new solution 復雜度 light n+1 java 空間復雜度 red

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        
        int n = nums.length;
        ArrayList<Integer> ans = new ArrayList<Integer>();
       boolean[] vis = new boolean[n+1];
        for(int i=0;i<n;i++){
            vis[nums[i]] = true;
        }
        for(int i=1;i<=n;i++){
            if(!vis[i]) ans.add(i);
        }
        
        
        return ans;
    }
}

  時間復雜度O(n),空間復雜度O(n)。空間復雜度O(1)的不會做- -待補

leetcode 448 Find All Numbers Disappeared in an Array