We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。
Input: [1,2,0] Output: 3
Input: [3,4,-1,1] Output: 2
Input: [7,8,9,11,12] Output: 1
O(n)
The text was updated successfully, but these errors were encountered:
/** * @param {number[]} nums * @return {number} */ var firstMissingPositive = function(nums) { for (let i = 0; i < nums.length; i++) { while (nums[i] > 0 && nums[i] <= nums.length && nums[nums[i] - 1] !== nums[i]) { swap(nums[i] - 1, i); } } for (let i = 0; i < nums.length; i++) { if (nums[i] !== i + 1) { return i + 1; } } return nums.length + 1; function swap(x, y) { const temp = nums[x]; nums[x] = nums[y]; nums[y] = temp; } };
function firstMissingPositive(nums: number[]): number { for (let i = 0; i < nums.length; i++) { while (nums[i] > 0 && nums[i] <= nums.length && nums[nums[i] - 1] !== nums[i]) { swap(nums[i] - 1, i); } } for (let i = 0; i < nums.length; i++) { if (nums[i] !== i + 1) { return i + 1; } } return nums.length + 1; function swap(x: number, y: number) { const temp = nums[x]; nums[x] = nums[y]; nums[y] = temp; } };
Sorry, something went wrong.
No branches or pull requests
41. First Missing Positive
给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。
Example 1
Example 2
Example 3
Note
O(n)
,并且只能使用常数级别的额外空间。The text was updated successfully, but these errors were encountered: