标签:
需详细分析!!!!!
代码:
1 #include<iostream> 2 #include<vector> 3 4 using namespace std; 5 6 void swap(int &a, int &b) 7 { 8 int c = a; 9 a = b; 10 b = c; 11 } 12 13 int firstMissingPositive(vector<int>& nums) 14 { 15 int L = nums.size(); 16 int i = 0; 17 while (i < L) 18 { 19 if ((nums[i] - 1 >= L)||(nums[i] - 1 < 0) ||(nums[i] == i + 1) || (nums[i] == nums[nums[i] - 1])) 20 i++; 21 else 22 swap(nums[i], nums[nums[i] - 1]); 23 } 24 for (i = 0; i < L; i++) 25 { 26 if (nums[i] != i + 1) 27 break; 28 } 29 return i + 1; 30 } 31 32 int main() 33 { 34 vector<int> nums = { 3, 4, -1, 1 }; 35 cout << firstMissingPositive(nums) << endl; 36 }
leetcode First Missing Positive
标签:
原文地址:http://www.cnblogs.com/chaiwentao/p/4508014.html