[LeetCode] 41. First Missing Positive

</br>


Given an unsorted integer array, find the first missing positive integer.

For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.


</br>

Solution

The solution can look like this, whenever the nums[i] != i+1, we swap the number with nums[nums[i]-1] to make sure that every number in the array is in the correct order.

Once the array is sorted, we can check whether the nums[i] == i+1, and the first place that does not satisfy this requirement should be the output value.
</br>
However, what if the value of certain numbers in the array is way too big, then the array may not have enough space to store all the value. For example,

nums[] = {2,3,5,999998};

We can solve this problem by ignoring numbers that is bigger than the size of the array.

But another problem also rises. How can we make sure that the numbers we ignored have no influence on the output?

Well, as the only scenario that this large number may influence the output is that all the numbers before this large number must be present, for example

nums[] = {2,3,4,5,...,999997,999998};

and in this case, the array itself would be equally large, and have no problem containing all numbers.
</br>
The code is shown as below.
Java

public class Solution {
    public int firstMissingPositive(int[] nums) {
        
        int i=0;
        
        while(i<nums.length){
            
            if(nums[i] <= 0 || nums[i] > nums.length || nums[i] == i+1){
                i++;
                continue;
            }
            
            if(nums[nums[i]-1] != nums[i]) 
                swap(nums, i, nums[i]-1);
            else i++;
        }
        
        i=0;
        while(i<nums.length){
            if(nums[i] != i+1)
                return i+1;
            i++;
        }
        return i+1;
    }
    
    private void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

</br>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市挟冠,隨后出現(xiàn)的幾起案子拆融,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡炭菌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門站叼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人菇民,你說我怎么就攤上這事尽楔。” “怎么了第练?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵阔馋,是天一觀的道長。 經(jīng)常有香客問我娇掏,道長呕寝,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任婴梧,我火速辦了婚禮下梢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘塞蹭。我一直安慰自己孽江,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布番电。 她就那樣靜靜地躺著岗屏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪漱办。 梳的紋絲不亂的頭發(fā)上这刷,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音娩井,去河邊找鬼暇屋。 笑死,一個(gè)胖子當(dāng)著我的面吹牛洞辣,可吹牛的內(nèi)容都是我干的率碾。 我是一名探鬼主播叔营,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼所宰!你這毒婦竟也來了绒尊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤仔粥,失蹤者是張志新(化名)和其女友劉穎婴谱,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體躯泰,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谭羔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了麦向。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瘟裸。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖诵竭,靈堂內(nèi)的尸體忽然破棺而出话告,到底是詐尸還是另有隱情,我是刑警寧澤卵慰,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布沙郭,位于F島的核電站,受9級(jí)特大地震影響裳朋,放射性物質(zhì)發(fā)生泄漏病线。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一鲤嫡、第九天 我趴在偏房一處隱蔽的房頂上張望送挑。 院中可真熱鬧,春花似錦暖眼、人聲如沸让虐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赡突。三九已至,卻和暖如春区赵,著一層夾襖步出監(jiān)牢的瞬間惭缰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來泰國打工笼才, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留漱受,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像昂羡,于是被迫代替她去往敵國和親絮记。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

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