*LeetCode-First Missing Positive
来源:互联网 发布:万国数据事件 编辑:程序博客网 时间:2024/05/22 15:59
从第一位开始 把每一位放到正确的位置上 使用swap 直到这一位的数字不是正的 或者已经是正确的数字 或者这里的数字超过了length 或者和需要换到的那一位数字重复了
这几个条件非常重要
还要注意最后的return值 以及数组是空的时候的return值
public class Solution { public int firstMissingPositive(int[] nums) { if ( nums == null || nums.length == 0 ) return 1; int n = nums.length; for ( int i = 0; i < n; i ++ ){ while ( nums [ i ] != i + 1 && nums [ i ] < n && nums[ i ] > 0 && nums [ nums [i] - 1 ] != nums [ i ]){ int temp = nums [ nums [i] - 1 ]; nums [ nums [i] - 1 ] = nums [ i ]; nums [ i ] = temp; } } for ( int i = 0; i < n; i ++ ){ if ( nums [ i ] != i + 1 ) return i + 1; } return n + 1; }}
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
- 1.3java语言基础(下)
- ios 修改navigationBar返回键 ' < ' 的颜色
- LeetCode 题解(264) : Verify Preorder Sequence in Binary Search Tree:
- 发布网站之后,局域网内其他机器无法访问的解决办法
- Laravel 500空白
- *LeetCode-First Missing Positive
- Homogenous Points(齐次坐标点)到底是什么
- [Leetcode]Set Matrix Zeroes
- C#学习之打开本地浏览器
- 排序_插入排序
- BOT EPC EMC
- 安装完最小化RHEL/Centos7后需要做的30件事
- Xcode中的全局异常断点
- Xcode调试非异常导致崩溃的程序