LeetCode First Missing Positive
来源:互联网 发布:windows 10 mobile安卓 编辑:程序博客网 时间:2024/06/07 22:58
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; }}
0 0
- LeetCode: First Missing Positive
- LeetCode First Missing Positive
- LeetCode: First Missing Positive
- [Leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- [Leetcode] First Missing Positive
- leetcode First Missing Positive
- LeetCode First Missing Positive
- [leetcode] first missing positive
- LeetCode: First Missing Positive
- [LeetCode]First Missing Positive
- LeetCode-First Missing Positive
- [leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- LeetCode - First Missing Positive
- LeetCode | First Missing Positive
- Leetcode: First Missing Positive
- Leetcode:First Missing Positive
- c语言中的运算优先级
- Oracle小心得+前言
- 《算法竞赛入门经典2ndEdition 》习题3-11 换低挡装置(Kickdown, Uva1588)
- Android Studio遇到Error:Failed to find: com.android.support:appcompat-v7:21.+
- 转:PC上 python 通过串口和 arduino 读写交互成功,哈哈,真好玩
- LeetCode First Missing Positive
- 非XML方式 部署redis缓冲 及自定义键值的方法如何在spring中声明它
- javaday22-IO序列流
- 用RxJava写的一个Android的小Demo
- windows 系统下CMD命令大全
- 数据结构 — 归并排序
- 零碎知识总结——学渣修行路
- 在openwrt下对华为WA633无线AP的千兆网卡驱动进行支持
- UVa-10129 单词