zoukankan      html  css  js  c++  java
  • LeetCode_448. Find All Numbers Disappeared in an Array

    448. Find All Numbers Disappeared in an Array

    Easy

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

    Find all the elements of [1, n] inclusive that do not appear in this array.

    Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

    Example:

    Input:
    [4,3,2,7,8,2,3,1]
    
    Output:
    [5,6]
    package leetcode.easy;
    
    public class FindAllNumbersDisappearedInAnArray {
    	public java.util.List<Integer> findDisappearedNumbers(int[] nums) {
    		boolean[] seenOrNot = new boolean[nums.length];
    		for (int i = 0; i < seenOrNot.length; i++) {
    			seenOrNot[i] = false;
    		}
    		for (int j = 0; j < nums.length; j++) {
    			seenOrNot[nums[j] - 1] = true;
    		}
    		java.util.ArrayList<Integer> unseen = new java.util.ArrayList<Integer>();
    		for (int i = 0; i < seenOrNot.length; i++) {
    			if (!seenOrNot[i]) {
    				unseen.add(i + 1);
    			}
    		}
    		return unseen;
    	}
    
    	@org.junit.Test
    	public void test() {
    		int[] nums = { 4, 3, 2, 7, 8, 2, 3, 1 };
    		System.out.println(findDisappearedNumbers(nums));
    	}
    }
    
  • 相关阅读:
    CCF CSP 201403-2 窗口
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
  • 原文地址:https://www.cnblogs.com/denggelin/p/11975795.html
Copyright © 2011-2022 走看看