clueless coding // TODO: be smarter

LeetCode Find All Numbers Disappeared in an Array: Java Solution



TL;DR Code Solution


public List<Integer> findDisappearedNumbers(int[] nums) {

    List<Integer> res = new ArrayList<Integer>();

    for (int i = 0; i < nums.length; i++) {
        int cur = Math.abs(nums[i]) - 1;
        if (nums[cur] > 0) {
            nums[cur] = -nums[cur];
        }
    }

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] > 0) {
            res.add(i + 1);    
        }
    }

    return res;

}