448. Find All Numbers Disappeared in an Array.C#

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]

直接利用HashSet可解:

public class Solution {
    public IList<int> FindDisappearedNumbers(int[] nums) {
        IList<int> result = new List<int>();
        HashSet<int> numsSet = new HashSet<int>(nums);
        for (int i = 1; i <= nums.Length; ++i) {
            if (!numsSet.Contains(i)) {
                result.Add(i);
            }
        }
        return result;
    }
}  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Given an array of integers where 1 ≤ a[i] ≤ n (n = size o...
    matrxyz阅读 158评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,537评论 0 13
  • 传说每棵樱花树下都埋葬着尸体,越是鲜艳的樱花,所埋葬的尸体也就越多。 而我只是为他伴行的樱花树 1.樱灵被树下那动...
    猫咪爬爬阅读 1,938评论 0 0
  • ;这是一个很久很久以前的一个故事。 每一个魔王成年前都要抓走一个公主,等待勇者将魔王战胜之后娶公主回去,或者魔王战...
    小小蛋糕糕阅读 850评论 0 0
  • “以为你长大了 只是表面的成熟罢了 有些事吃了亏是不能回头的 可以问你爷爷 他的一句话 没进大学的门 你...
    柚子溪阅读 177评论 0 0