一天一道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.

(二)解题

  1. /*
  2. 首先对初始vector进行排序,然后设定一个target=1
  3. 从第一个大于0的数开始,如果等于target的数后就+1,直到下一个数不能于target为止
  4. 如果不能于就返回target
  5. 如果数组中找完了都没有返回,则返回target+1;
  6. 当然这里要考虑一种特殊情况:数组中有重复的数需要排除
  7. */
  8. class Solution {
  9. public:
  10. int firstMissingPositive(vector<int>& nums) {
  11. sort(nums.begin(),nums.end());
  12. int target = 1;
  13. for(int i = 0 ; i < nums.size() ; i++)
  14. {
  15. if(nums[i]>0)
  16. {
  17. if(nums[i] == target)
  18. {
  19. target++;//注意这里已经+1了,最后返回直接返回target
  20. }
  21. else if(i>0 &&nums[i]!=nums[i-1])//排除重复数字
  22. {
  23. return target;
  24. }
  25. }
  26. }
  27. return target;//如果找不到空缺的就返回target+1
  28. }
  29. };

【一天一道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. MySQL 连接的使用

    MySQL 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JOIN ...

  2. DuKBitmapImages 图片压缩处理技术

    Android图片压缩上传系列 *压缩中的问题: --图片压缩上如果存在问题,势必造成消耗大量的流量,下载图片的速度慢等影响产品性能,那么如何解决?请看下面: 压缩图片一共多少张?一起压缩?分开压缩? ...

  3. Bootstrap3 排版-列表

    无序列表 排列顺序无关紧要的一列元素. <ul> <li>...</li> </ul> 有序列表 顺序至关重要的一组元素. <ol> < ...

  4. 六星经典CSAPP-笔记(11)网络编程

    六星经典CSAPP-笔记(11)网络编程 参照<深入理解计算机系统>简单学习了下Unix/Linux的网络编程基础知识,进一步深入学习Linux网络编程和TCP/IP协议还得参考Steve ...

  5. ObjectOutputStream 和 ObjectInputStream的使用

    一.看一下API文档 ObjectOutputStream : ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream.可以使用 ObjectInp ...

  6. [boost] build boost with intel compiler 16.0.XXX

    Introduction There are few information about how to compile boost with Intel compiler. This article ...

  7. iOS开发基础之开发证书的说明和发布

    1.首先通过钥匙串访问--证书助理--从证书颁发机构请求证书--填写证书信息(邮箱,常用名称,存储到磁盘)--存储为(自定义名称.certSigningReuqest,简称CSR文件,只是为了提交到苹 ...

  8. Android Studio 2.2 新功能详解

    Tamic /文 -译 http://blog.csdn.net/sk719887916/article/details/52672688 Android的Studio 2.2 已经可以在官网下载了. ...

  9. Android自定义View(一、初体验自定义TextView)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客] 目录: 继承View重写onDraw方法 自 ...

  10. [django] 利用多线程增加异步任务

    看到django异步大家的反应应该是celery这种消息队列组件,现在用的最多的最推荐的也是这种方式.然而我这需求就是请求来了,执行一个小程序,但是又不能确定这个小程序啥时候执行完,响应又要及时,丢给 ...