Algorithm :Missing Number

Q: Missing Number

Description:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

A1: Bit Manipulation

Intuition
We can harness the fact that XOR is its own inverse to find the missing element in linear time.

Algorithm
Because we know that nums containsnnumbers and that it is missing exactly one number on the range [0..n-1] , we know thatndefinitely replaces the missing number in nums. Therefore, if we initialize an integer tonand XOR it with every index and value, we will be left with the missing number. Consider the following example (the values have been sorted for intuitive convenience, but need not be):

Index 0 1 2 3
Value 0 1 3 4
WX20171111-150701@2x

Python3

class Solution:
    def missingNumber(self, nums):
        missing = len(nums)
        for i, num in enumerate(nums):
             missing ^= i ^ num
        return missing

Complexity Analysis

  • Time complexity : O(n)

    Assuming that XOR is a constant-time operation, this algorithm does constant work on nn iterations, so the runtime is overall linear.

  • Space complexity : O(1)

    This algorithm allocates only constant additional space.

A2:Gauss' Formula

Intuition
One of the most well-known stories in mathematics is of a young Gauss, forced to find the sum of the first 100 natural numbers by a lazy teacher. Rather than add the numbers by hand, he deduced a closed-form expression for the sum, or so the story goes. You can see the formula below:

QQ20171112-092718@2x.png

Algorithm

We can compute the sum of nums in linear time, and by Gauss' formula, we can compute the sum of the first n natural numbers in constant time. Therefore, the number that is missing is simply the result of Gauss' formula minus the sum of nums, as nums consists of the first n natural numbers minus some number.

Python3

class Solution:
    def missingNumber(self, nums):
        expected_sum = len(nums)*(len(nums)+1)//2
        actual_sum = sum(nums)
        return expected_sum - actual_sum

Complexity Analysis

  • Time complexity : O(n)

    Although Gauss' formula can be computed in O(1) time, summing nums costs us O(n) time, so the algorithm is overall linear. Because we have no information about which number is missing, an adversary could always design an input for which any algorithm that examines fewer thann numbers fails. Therefore, this solution is asymptotically optimal.

  • Space complexity : O(1)
    This approach only pushes a few integers around, so it has constant memory usage.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末败匹,一起剝皮案震驚了整個(gè)濱河市裂允,隨后出現(xiàn)的幾起案子园担,更是在濱河造成了極大的恐慌,老刑警劉巖齐苛,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡宜猜,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門硝逢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)姨拥,“玉大人,你說(shuō)我怎么就攤上這事渠鸽〗形冢” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵徽缚,是天一觀的道長(zhǎng)憨奸。 經(jīng)常有香客問(wèn)我,道長(zhǎng)凿试,這世上最難降的妖魔是什么排宰? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮那婉,結(jié)果婚禮上板甘,老公的妹妹穿的比我還像新娘。我一直安慰自己详炬,他們只是感情好盐类,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著呛谜,像睡著了一般在跳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上隐岛,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天猫妙,我揣著相機(jī)與錄音,去河邊找鬼聚凹。 笑死吐咳,一個(gè)胖子當(dāng)著我的面吹牛逻悠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播韭脊,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼囚衔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼钥弯!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤谬返,失蹤者是張志新(化名)和其女友劉穎智亮,沒(méi)想到半個(gè)月后酌呆,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體榔幸,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年篓吁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了茫因。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡杖剪,死狀恐怖冻押,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情盛嘿,我是刑警寧澤洛巢,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站次兆,受9級(jí)特大地震影響稿茉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芥炭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一漓库、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧园蝠,春花似錦渺蒿、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)良瞧。三九已至陪汽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間褥蚯,已是汗流浹背挚冤。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赞庶,地道東北人训挡。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓澳骤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親澜薄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子为肮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容

  • 在暖暖晴天的鼓浪嶼,我給未來(lái)的自己寫(xiě)了幾封信肤京,內(nèi)容太多颊艳,已然記不得。 大抵是愿歲月無(wú)恙忘分,只是舊了時(shí)光之類的棋枕。 深知...
    我不是妖精閱讀 382評(píng)論 0 3
  • 1.少有人走得路 (完,9.12重看)2.烏有鄉(xiāng)(完)3.番茄工作法(完)4.熱夜之夢(mèng)(完)5.利維坦 (中斷)6...
    scarqin閱讀 187評(píng)論 0 0
  • 你說(shuō)我變了妒峦,卻忘了重斑,如今,還有誰(shuí)會(huì)傻傻地在原地等你呢? 記不住有多久總是一個(gè)人在家里默默做菜等待肯骇,直到深夜蜷縮在床...
    94暖暖陽(yáng)光閱讀 194評(píng)論 3 2