一天一道LeetCode系列

(一)题目

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.

(二)解题

/*
首先对初始vector进行排序,然后设定一个target=1
从第一个大于0的数开始,如果等于target的数后就+1,直到下一个数不能于target为止
如果不能于就返回target
如果数组中找完了都没有返回,则返回target+1;
当然这里要考虑一种特殊情况:数组中有重复的数需要排除
*/
class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int target = 1;
        for(int i = 0 ; i < nums.size() ; i++)
        {
            if(nums[i]>0)
            {
                if(nums[i] == target)
                {
                    target++;//注意这里已经+1了,最后返回直接返回target
                }
                else if(i>0 &&nums[i]!=nums[i-1])//排除重复数字
                {
                    return target;
                }
            }
        }
        return target;//如果找不到空缺的就返回target+1
    }
};

【一天一道LeetCode】#41. First Missing Positive的更多相关文章

  1. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  2. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  3. [LeetCode] 41. First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  4. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  5. Java [Leetcode 41]First Missing Positive

    题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...

  6. [leetcode]41. First Missing Positive第一个未出现的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  8. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

    First Missing Positive  Given an unsorted integer array, find the first missing positive integer. Fo ...

  9. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

  10. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

随机推荐

  1. ACM Piggy Bank

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  2. PHP 5 String 函数

    PHP 5 String 函数 PHP String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. 函数 描述 addcslashes() 返回在指定的字符前添加反斜杠的字符串. add ...

  3. Python pandas.io.data 模块迁移

    这段时间用pandas做数据分析, import pandas.io.data as web 然后得到下面的错误提示 "The pandas.io.data module is moved ...

  4. GDAL C#读取shp中文属性值乱码问题

    GDAL的C#版本读取shp中,如果属性值中含有中文,读出来有可能是乱码的问题,根据SWIG生成的C#代码调试发现问题所在,在Ogr.cs文件中有这么一个函数,代码如下: internal stati ...

  5. 操作系统内核Hack:(三)引导程序制作

    操作系统内核Hack:(三)引导程序制作 关于本文涉及到的完整源码请参考MiniOS的v1_bootloader分支. 1.制作方法 现在我们已经了解了关于BootLoader的一切知识,让我们开始动 ...

  6. solr和solrcloud

    Solr = Lucene + Http(Servlet/REST) + Schema.xml+Solrconfig.xml Solr = SolrSingle + Solr MutilCore = ...

  7. [Angular2]eclipse中angular2开发环境的搭建

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan 环境准备 1.eclipse neon 2.网络连接 插件地址 eclipse的插件市场地址: htt ...

  8. 针对于Python的OpenCV环境搭建

    OpenCV 依赖 下载OpenCV 配置 总结 给Python搭建opencv的环境还真是略嫌麻烦,于是做下笔记,以备不时之需. OpenCV 依赖 opencv有些依赖,我们必须安装一下,否则接下 ...

  9. 如何对n个大小都小于100的整数进行排序,要求时间复杂度O(n),空间复杂度O(1)。

    提示:hash表 #include <iostream> using namespace std; #define N 100 #define RANGE 100 int* getRand ...

  10. REFRESH删除POSTGRESQL

    sudo apt-get install python-psycopg2sudo apt-get install postgresql sudo su - postgres createuser -- ...