97 ms Java sulotion


  • 0
    J

    I am a novice java.How can I optimize my method?

    public class Solution {
        public List<Integer> findDisappearedNumbers(int[] nums) {
            List<Integer> res_list = new ArrayList<Integer>();
            Arrays.sort(nums);
            int max = nums.length;
            
            Set<Integer> nums_set = new HashSet<>();
            for(int n : nums){
                nums_set.add(n);
            }
            
            for(int i=1;i<=max;i++){
                if(!nums_set.contains(i)){
                    res_list.add(i);
                }
            }
            
            return res_list;
        }
    }
    

Log in to reply
 

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