这样就代替了解法二中的tag数组了。 classSolution {public:intfirstMissingPositive(intA[],intn) {if(n ==0)return1;//if A[i] is negative, i+1 exists in original A//partition, non-negative onlyintlow =0;inthigh = n-1;intend = n-1;while(low <=high) {while(low <= high && A[low...
PS:在下面的代码中,数组的下标从0开始。 1classSolution {2public:3intfirstMissingPositive(intA[],intn) {4inti=0;5while(i<n)6{7if(A[i]<=0|| A[i]>n || A[i] == i+1) i++;// 无法继续进行交换8else9{10inttarget=A[i]-1;11if(A[i]!=A[target]) swap(A[i],A[target]);1...
class Solution { public: int firstMissingPositive(vector<int>& nums) { bucket_sort(nums); for(int i = 0; i < nums.size(); ++i) { if(nums[i] != i + 1) { return i + 1; } } return nums.size() + 1; } private: void bucket_sort(vector<int>& nums) { for(int i = 0...
package leetcode_50; /*** * * @author pengfei_zheng * 找到第一个丢失的正数 */ public class Solution41 { public static int firstMissingPositive(int[] nums) { int i = 0; while(i < nums.length){ if(nums[i] == i+1 || nums[i] <= 0 || nums[i] > nums.length) i++; else...
1 <= nums.length <= 5 * 105 -231<= nums[i] <= 231- 1 题目大意:找到第一个缺失的正整数 解题思路:先讲数组放到map中,然后依次对比map中是否存在i,如果不存在就返回结果 classSolution(object):deffirstMissingPositive(self,nums):""":type nums: List[int]:rtype: int"""ifnums[0]==1andnums...
publicclassSolution{/** * 157 / 157 test cases passed. * Status: Accepted * Runtime: 14 ms * * @param nums * @return */publicintfirstMissingPositive(int[]nums){for(inti=0;i<nums.length;i++){if(nums[i]!=i+1){// swap nums[i] with nums[nums[i] - 1]intcur=nums[i]-1;if...
fromtypingimportListclassSolution:deffirstMissingPositive(self,nums:List[int])->int:foriinrange(len(nums)):while0<nums[i]<=len(nums)andnums[nums[i]-1]!=nums[i]:nums[nums[i]-1],nums[i]=nums[i],nums[nums[i]-1]foriinrange(0,len(nums)):ifnums[i]!=(i+1):returni+1returnlen...
代码: classSolution {public:intfirstMissingPositive(vector<int>&nums) {for(inti =0; i < nums.size(); ++i) {while(nums[i] >0&& nums[i] <= nums.size() && nums[nums[i] -1] !=nums[i]) //其实这里不要nums[i] > 0 也可以 就是会慢一点 ...
publicclassSolution{ publicintfirstMissingPositive(int[]nums){ if(nums==null||nums.length==0){ return1; } // 创建一个查找表,用来记录 1~nums.length 中数字出现的情况 boolean[]exist=newboolean[nums.length]; for(inti=0;i<nums.length;i++){ ...
public class Solution { public int firstMissingPositive(int[] nums) { for (int i = 0; i < nums.length; ) { int n = nums[i]; if (n >= 1 && n <= nums.length + 1 && nums[n - 1] != n) {//这个数在答案区间 int tmp = nums[n - 1]; ...