题目要求

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.

在数组中找到第一个漏掉的正整数。如果可以的话,使用O(N)的时间复杂度和O(1)的空间复杂度。

思路一:暴力排序后寻找

排序后寻找显然是最快的。排序的时间复杂度要依靠java底层的依赖。之后再用O(N)的时间复杂度找到第一个漏掉的整数后即退出遍历。

    public int firstMissingPositive(int[] nums) {int size = nums.length;if(size == 0) return 1;Arrays.sort(nums);int positive = 1;for(int i = 0 ; i<size ; i++){if(nums[i]<positive) continue;if(nums[i]>positive) return positive;positive++;}return positive;}

思路二:O(1)空间复杂度

要实现这种空间复杂度,一般需要有限数量的临时变量来记录遍历的有效范围,再利用原有题目中的数据结构来存储其余的临时变量。这些临时变量可以是排除出的量,也可以是有效量。在这里我用leftPointer记录有效数字的开始下标(即将无效数字转移到leftPointer左侧),利用maxPositive记录数组最大有效整数(换句话说,如果一个数组的大小为9,则该数组的最大first missing positive integer即为10,一旦数组中出现重复或是小于1或是大于9的数字,该数字即为无效数字)。当遇到的数字为有效数字时,则将该数字放到对应当前起始下标leftPointer其相应的位置上。

    public int firstMissingPositive2(int[] nums){int size = nums.length;int positive = 1;int leftPointer = 0;int maxPositive = size;while(leftPointer<size){if(nums[leftPointer] == positive){leftPointer++;positive++;}else if(nums[leftPointer] > maxPositive || nums[leftPointer] < positive || nums[leftPointer]==nums[leftPointer+nums[leftPointer]-positive]){leftPointer++;maxPositive--;}else{int temp = nums[leftPointer];nums[leftPointer] = nums[leftPointer+temp-positive];nums[leftPointer+temp-positive] = temp;}}return positive;}    


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

leetcode 41. First Missing Positive 1相关推荐

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

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

  2. [LeetCode]41.First Missing Positive

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

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

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

  4. [leetcode]41. First Missing Positive

    题目地址 https://leetcode.com/problems/first-missing-positive/ 题目大意 一个整数数组,里面数字是无序的,在O(n)的时间复杂度,O(1)的空间复 ...

  5. leetcode 41. First Missing Positive

    https://www.cnblogs.com/grandyang/p/4395963.html https://www.jianshu.com/p/cf82ce91dc3d 错误解法1: [1,1] ...

  6. LeetCode题解41.First Missing Positive

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

  7. LeetCode 41. First Missing Positive--Python 解法--数学题-找到不存在的最小正整数-O(1)空间复杂度

    题目地址:First Missing Positive - LeetCode Given an unsorted integer array, find the smallest missing po ...

  8. 41. First Missing Positive

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

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

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

最新文章

  1. .NET CF WM上创建快捷方式
  2. 彩色图批量转换成灰度图、批量格式转换、批量重命名
  3. 网页脚本基本java语法_JSP 基础语法
  4. 禁止UDP端口引起DNS错误导致邮局无法外发的故障
  5. docker环境下指定jvm参数
  6. python区域增长_Python – 有效地为高密度区域创建密度图,稀疏区域的点
  7. 改进的SVN的Commit权限控制
  8. XCTF-MISC-新手区-功夫再高也怕菜刀
  9. PHP_SELF变量解析和重复路径解决
  10. media player 控件播放音乐与视频 0130 winform
  11. c++ 类全局变量_static在C和C++中的用法总结
  12. 云计算的核心技术有哪些?
  13. Postman 汉化包(设置中文)
  14. 改变生活的态度,突破瓶颈
  15. BUUCTF Misc 被劫持的神秘礼物
  16. 空间发表说说html页面实现原理,空间说说还能这样玩?jquery开发腾讯QQ空间说说发表效果...
  17. python第二周基本图形绘制
  18. Failed to download https://chrome-infra-packages.appspot.com/dl/flutter/web/canvaskit_bundle/+/8MSYG
  19. SSCoin交易开放时间及未来价值
  20. 中核科技:科技匠心 智启未来

热门文章

  1. Hadoop中RPC机制详解之Server端
  2. !--[if IE]….![endif]-- (!--[if !IE]||![endif]--)的用法
  3. 一起播四个视频的AVS脚本
  4. Java 动态代理实现
  5. 正则表达式在JS中的应用
  6. FZYZ-2071 A Simple Math Problem IX
  7. 如何在博客等文章中添加带有滚动条的文本框
  8. 使用VirtualBox SDK之初步编译
  9. jQuery遮罩层(转)
  10. JS正则限制价格输入 0.01~ 99999.99