Time O(N) and space (1)


  • 1
    T

    '''
    public class Solution {
    // O(n)
    public List<Integer> findDisappearedNumbers(int[] nums) {
    List<Integer> result = new ArrayList<Integer>();
    if (nums == null || nums.length == 0) {
    return result;
    }

        for(int i = 0; i < nums.length; i++) {
            int val = Math.abs(nums[i]) - 1;
            if (nums[val] < 0) {
                continue;
            }
            
            nums[val] = nums[val] * -1;
        }
        
        for(int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                result.add(i + 1);
            }
        }
        return result;
    }
    

    }
    '''


  • 0
    L

    I didn't understand what does the val and nums[val] mean?


Log in to reply
 

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