Saturday, January 19, 2019

448. Find All Numbers Disappeared in an Array



69.09 %
class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        // If we see this number, we mark the number that at this index as negative
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i]) - 1;
            if (nums[index] > 0) {
                nums[index] = -nums[index];
            }
        }
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                result.add(i + 1);
            }
        }
        return result;
    }
}

No comments:

Post a Comment