Java AC Simple


  • 0
    A
    public List<Integer> findDisappearedNumbers(final int[] nums) {
            for (int i = 0; i < nums.length; i++) {
                int index = Math.abs(nums[i]) - 1;
                if (nums[index] > 0) {
                    nums[index] = -nums[index];//mark the index as found
                }
            }
            
            List<Integer> missing = new ArrayList<>();
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] > 0) {
                    missing.add(i+1);
                } else {
                    nums[i] = -nums[i];//revert the marking
                }
            }
            
            return missing;
        }
    

Log in to reply
 

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