• 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));
    	}
    }
    
  • 相关阅读:
    我所知道的数据库8-DML语言
    我所知道的数据库7-DDL语言(续2)
    CSS3 3D transform变换
    深入理解Node.js的异步编程风格(转载)
    ECMAscript
    JavaScript高级部分概念用法
    前端工程师面试题汇总
    10个最常见的HTML5中的面试题及答案
    js事件流
    本地储存
  • 原文地址:https://www.cnblogs.com/denggelin/p/11975795.html
Copyright © 2020-2023  润新知