LeetCode 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.
题意:给出一个数组,求其第一个缺失的整数
思路:从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相关推荐
- [LeetCode] First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- LeetCode: First Missing Positive 解题报告
Q: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...
- leetcode - First Missing Positive
题目描述:点击此处 利用到原来的数组空间,不知道符不符合题意. 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int ...
- 【排序+难题】LeetCode 41. First Missing Positive
LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...
- [LeetCode]41.First Missing Positive
[题目] Given an unsorted integer array, find the first missing positive integer. For example, Given [1 ...
- leetcode 41. First Missing Positive 1
题目要求 Given an unsorted integer array, find the first missing positive integer.For example, Given [1, ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
最新文章
- 什么是php递归算法_PHP递归算法(一)
- Unity导出apk出现的问题,JDK,Android SDK,NDK,无“安装模块”
- No module named 'xxx’
- 升级Firefox8后watir-webdriver出现错误“unable to obtain stable firefox connection in 60 seconds”...
- videoJS如何用
- Spring教程--IOC(注解方式)和整合junit
- 《大道至简》第二篇读后感
- spring jpa.踩坑经验分享
- 关停近300家门店,海底捞:苦果只能一口一口咽下去
- 自定义导航--wx.getMenuButtonBoundingClientRect() 万机兼容
- vue data 值如何渲染_vue源码阅读复盘-watcher模块
- 荒野大镖客fc java_FC荒野大镖客无敌版
- B2B跨境电子商务平台综合服务解决方案
- 智能制造+机器视觉技术培训研讨会
- CRM系统创建营销活动
- 3d打印技术改变生活的影响
- 1665 完成所有任务的最少初始能量
- java中的odb_obd适配器的初始化
- 如何检测摄像头是否被占用
- 学go还是python?
热门文章
- Linux修改文件及文件夹权限
- android app 历史版本,怎么找到App的所有历史版本,以及每次改版的变更点呢?
- python入门只需20分钟-史上最详细python学习路线-从入门到精通,只需5个月时间...
- 怎么用python制作简单的程序-神级程序员教你如何用python制作一个牛逼的外挂!...
- python语言入门p-python初学者怎么入门
- python爬虫图片-Python图片爬取方法总结
- 转行学python后悔了-转行学Python可以吗?
- python恶搞表情包-Python开发个人专属表情包网站,表情在手,天下我有
- python和c语言有什么关系-Python与C语言有什么区别?
- python小游戏编程实例-10分钟教你用Python写一个贪吃蛇小游戏,适合练手项目