[Java] Mark occured number in index with -1 multiplication / T : O(N), S : O(1)


  • 0
    J
    public class Solution {
        public List<Integer> findDisappearedNumbers(int[] nums) {
            List<Integer> result = new ArrayList<Integer>();
            for(int i = 0; i < nums.length; i++){
                int index = Math.abs(nums[i]) - 1; 
                if(nums[index] > 0) 
                    nums[index] *= -1;
            }
            
            for(int i = 0; i < nums.length; i++){
                if(nums[i] > 0){
                    result.add(i+1);
                }
            }
            
            return result;
        }
    }
    

Log in to reply
 

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