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,依次类推。在查找的过程中有两种情况

(1)如果nums[i] 不等于要找的数,i++

(2)如果相等,就将nums[i]与要找的数所在的下标交换,并且从要找的数的下标开始继续查找,同时将要查找的数加1

代码如下:

class Solution
{public int firstMissingPositive(int[] nums){int result = 1;for (int i = 0; i < nums.length;){if (nums[i] != result) i++;else{int tmp = nums[result - 1];nums[result - 1] = nums[i];nums[i] = tmp;i = result++;}}return result;}
}

LeetCode First Missing Positive相关推荐

  1. [LeetCode] First Missing Positive

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

  2. LeetCode: First Missing Positive 解题报告

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

  3. leetcode - First Missing Positive

    题目描述:点击此处 利用到原来的数组空间,不知道符不符合题意. 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int ...

  4. 【排序+难题】LeetCode 41. First Missing Positive

    LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...

  5. [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 1

    题目要求 Given an unsorted integer array, find the first missing positive integer.For example, Given [1, ...

  7. LeetCode题解41.First Missing Positive

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

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

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

  9. 【leetcode】First Missing Positive(hard) ☆

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

最新文章

  1. 什么是php递归算法_PHP递归算法(一)
  2. Unity导出apk出现的问题,JDK,Android SDK,NDK,无“安装模块”
  3. No module named 'xxx’
  4. 升级Firefox8后watir-webdriver出现错误“unable to obtain stable firefox connection in 60 seconds”...
  5. videoJS如何用
  6. Spring教程--IOC(注解方式)和整合junit
  7. 《大道至简》第二篇读后感
  8. spring jpa.踩坑经验分享
  9. 关停近300家门店,海底捞:苦果只能一口一口咽下去
  10. 自定义导航--wx.getMenuButtonBoundingClientRect() 万机兼容
  11. vue data 值如何渲染_vue源码阅读复盘-watcher模块
  12. 荒野大镖客fc java_FC荒野大镖客无敌版
  13. B2B跨境电子商务平台综合服务解决方案
  14. 智能制造+机器视觉技术培训研讨会
  15. CRM系统创建营销活动
  16. 3d打印技术改变生活的影响
  17. 1665 完成所有任务的最少初始能量
  18. java中的odb_obd适配器的初始化
  19. 如何检测摄像头是否被占用
  20. 学go还是python?

热门文章

  1. Linux修改文件及文件夹权限
  2. android app 历史版本,怎么找到App的所有历史版本,以及每次改版的变更点呢?
  3. python入门只需20分钟-史上最详细python学习路线-从入门到精通,只需5个月时间...
  4. 怎么用python制作简单的程序-神级程序员教你如何用python制作一个牛逼的外挂!...
  5. python语言入门p-python初学者怎么入门
  6. python爬虫图片-Python图片爬取方法总结
  7. 转行学python后悔了-转行学Python可以吗?
  8. python恶搞表情包-Python开发个人专属表情包网站,表情在手,天下我有
  9. python和c语言有什么关系-Python与C语言有什么区别?
  10. python小游戏编程实例-10分钟教你用Python写一个贪吃蛇小游戏,适合练手项目