Given an unsorted integer array, find the first missing positive integer.
来源:互联网 发布:java 浏览器下载文件 编辑:程序博客网 时间:2024/06/06 01:37
/*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.*/public class FindFirstLostPositive {public static void main(String[] args) {System.out.println(new FindFirstLostPositive().firstMissingPositive(new int[]{-1,-2,-3,1,2,3,4,5,6,7,8,9}));} public int firstMissingPositive(int[] nums) { if(nums.length==0||nums==null) return 1; //1放在nums[0],2放在nums[1] for(int i = 0;i<nums.length;i++) { while(nums[i]!=i+1) { if(nums[i]>=nums.length||nums[i]<=0||nums[i]==nums[nums[i]-1]) break; int temp = nums[i]; nums[i] = nums[temp-1]; nums[temp-1] = temp; } } for(int i = 0;i<nums.length;i++) { if(nums[i]!=i+1) return i+1; } return nums.length+1; }}
0 0
- First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo
- Given an unsorted integer array, find the first missing positive integer.
- Find the smallest positive number missing from an unsorted array
- Given an array say [9,20,-2,-45,23,5,1], find the minimum positive missing number from the array.
- [LeetCode]041-First Missing Positive Integer
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Find the k-th minimal element of an unsorted array
- Find the kth largest element in an unsorted array
- First Missing Integer
- You’re given an array containing both positive and negative integers and required to find the sub-a
- Find an integer not among four billion given ones
- python求解LeetCode习题Find the First Missing Positive Number
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- given two unordered list find the greatest common integer
- Given a positive integer N, you should output the most right digit of N^N.
- Find the missing integer from 0 to n
- Find the missing integer----programming pearls(second chapter)
- 寻找数组中缺少的整数(Find the missing integer)
- PopupListView(ListView的Item点击弹出和退回到原来位置)
- 规范Python程序配置文件读写
- 后端知识体系--一次完整的HTTP请求
- 最简单的基于FFmpeg的libswscale的示例
- css3动画animation属性
- Given an unsorted integer array, find the first missing positive integer.
- 输入三角形的三个边,判断是否为等边三角形
- 在CentOS7上安装MySQL
- iOS开发之通知中心、KVC、KVO
- 31.leetcode题目226: Invert Binary Tree
- 服务器监控
- Android推送技术研究
- Codeforces Round #346 (Div. 2) C.Tanya and Toys
- j2se数据类型