[LeetCode]41.First Missing Positive

简介:

【题目】

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.

【分析】

类似于桶排序。

每当A[i] != i+1时,即A[i]不在正确的位置上,需要交换到排序数组中他相应的位置上去。

交换A[i]与A[A[i]-1],直到无法交换。

【代码】

/*********************************
*   日期:2015-01-13
*   作者:SJF0115
*   题目: 41.First Missing Positive
*   来源:https://oj.leetcode.com/problems/first-missing-positive/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
using namespace std;

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        if(A == NULL || n <= 0){
            return 1;
        }//if
        // 交换到在排序数组中应有的位置
        for(int i = 0;i < n;i++){
            // 交换直到不能交换
            while(A[i] != i+1){
                // 是否需要交换
                if(A[i] <= 0 || A[i] > n || A[i] == i+1 || A[i] == A[A[i]-1]){
                    break;
                }//if
                // 交换
                int tmp = A[i];
                A[i] = A[tmp-1];
                A[tmp-1] = tmp;
            }//while
        }//for
        // First Missing Positive
        for(int i = 0;i < n;i++){
            if(A[i] != i+1){
                return i+1;
            }//if
        }//for
        return n+1;
    }
};

int main(){
    Solution solution;
    int A[] = {1,1};
    cout<<solution.firstMissingPositive(A,2)<<endl;
    return 0;
}


【分析二】

题目的最后一行,要求O(n)实际上暗示了用hash,但是又说要contant space,就没法再开新空间来建hash。
正好这个题目中处理的是1到n的数据,提供了一个将输入的数组同时用作hash表的可能性。
于是算法就是:

  1. 第一遍扫描排除所有非正的数,将它们设为一个无关紧要的正数(n+2),因为n+2不可能是答案
  2. 第二遍扫描,将数组作为hash表来使用,用数的正负来表示一个数是否存在在A[]中。
    当遇到A[i],而A[i]属于区间[1,n],就把A中位于此位置A[i] – 1的数置翻转为负数。
    所以我们取一个A[i]的时候,要取它的abs,因为如果它是负数的话,通过步骤一之后,只可能是我们主动设置成负数的
  3. 第三遍扫描,如果遇到一个A[i]是正数,说明i+1这个数没有出现在A[]中,只需要返回即可。
  4. 上一步没返回,说明1到n都在,那就返回n+1

【代码二】

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        if(A == NULL || n <= 0){
            return 1;
        }//if
        int impossValue = n + 2;
        //first run, turn every negetive value into an impossible positive value
        //make every value in A is positive
        for(int i = 0;i < n;i++){
            if(A[i] <= 0){
                A[i] = impossValue;
            }//if
        }//for
        //second run, make A[] as a hash table, A[i] indicate the presence of i + 1
        //the way is that, if k in [1,n] is in A[], then turn A[k -1] to negetive
        for(int i = 0;i < n;i++){
            int value = abs(A[i]);
            if(value <= n){
                A[value-1] = -abs(A[value-1]);
            }//if
        }//for
        //third run, if A[i] is positive, from step 2, we know that i + 1 is missing.
        for(int i = 0;i < n;i++){
            if(A[i] > 0){
                return i+1;
            }//if
        }//for
        //all int from 1 to n is present, then return n + 1
        return n+1;
    }
};




目录
相关文章
|
索引
LeetCode 387. First Unique Character in a String
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
283 0
LeetCode 387. First Unique Character in a String
|
测试技术 API
LeetCode 278. First Bad Version
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
144 0
LeetCode 278. First Bad Version
|
测试技术 API
LeetCode 278. 第一个错误的版本 First Bad Version
LeetCode 278. 第一个错误的版本 First Bad Version
|
数据库
LeetCode(数据库)- First and Last Call On the Same Day
LeetCode(数据库)- First and Last Call On the Same Day
172 0
LeetCode之First Unique Character in a String
LeetCode之First Unique Character in a String
189 0
|
Java 索引 Python
LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母。
917 0
|
算法
LeetCode 41 First Missing Positive(丢失的第一个正数)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52435582 翻译 给定一个未排序的整型数组,找出第一个丢失的正数。
1126 0
|
API 移动开发
LeetCode 278 First Bad Version(第一个坏版本)(二分法)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611643 翻译 你是一个产品经理,目前正在带领团队去开发一个新产品。
952 0