【Leetcode】Set Mismatch

The set?S?originally contains numbers from 1 to?n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to?another?number in the set, which results in repetition of one number and loss of another number.

Given an array?nums?representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

1 第一種方法,用m = collection.Counter[nums]的對(duì)nums計(jì)數(shù),再遍歷nums中的數(shù),找出m[num]==2的num即是第一個(gè)數(shù)

2 得出原始list的sum,再得出error list的sum,通過(guò)和first數(shù)結(jié)合算出second數(shù)。這種方法時(shí)間復(fù)雜度只超過(guò)了23%

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

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • 它就像一位純潔天真的害羞少女,雖然她不是那種立刻會(huì)吸引住人們目光的女子,但是,越仔細(xì)看就越能發(fā)現(xiàn)她的美麗和優(yōu)點(diǎn)。純...
    阿溫_ecdd閱讀 576評(píng)論 0 0
  • 有時(shí)候常常會(huì)想,我們是不是對(duì)自己太過(guò)嚴(yán)苛了?我們熬夜,早起,要做這個(gè)要做那個(gè)。 看到好的書法作品,自己也想要寫出...
    松鼠會(huì)唱歌閱讀 2,645評(píng)論 2 3
  • 我常常羨慕一種人,不論發(fā)生事情都不會(huì)放在心上,該吃吃,該玩玩,該睡睡。似乎天塌下來(lái)也不會(huì)影響他的心情。俗稱心大。 ...
    北佳人閱讀 977評(píng)論 1 2
  • 在剛剛結(jié)束的超級(jí)750丹麥羽毛球公開(kāi)賽上,中國(guó)臺(tái)北選手“影帝”周天成1∶2不敵日本選手中桃田賢斗,獲得亞軍...
    俗而不雅閱讀 289評(píng)論 0 1

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