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;
    }
}  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Given an array of integers where 1 ≤ a[i] ≤ n (n = size o...
    matrxyz閱讀 171評論 0 0
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,894評論 0 13
  • 傳說每棵櫻花樹下都埋葬著尸體,越是鮮艷的櫻花,所埋葬的尸體也就越多。 而我只是為他伴行的櫻花樹 1.櫻靈被樹下那動...
    貓咪爬爬閱讀 2,007評論 0 0
  • ;這是一個很久很久以前的一個故事。 每一個魔王成年前都要抓走一個公主,等待勇者將魔王戰(zhàn)勝之后娶公主回去,或者魔王戰(zhàn)...
    小小蛋糕糕閱讀 905評論 0 0
  • “以為你長大了 只是表面的成熟罷了 有些事吃了虧是不能回頭的 可以問你爺爺 他的一句話 沒進大學(xué)的門 你...
    柚子溪閱讀 193評論 0 0

友情鏈接更多精彩內(nèi)容