给你一个未排序的整数数组 nums
,请你找出其中没有出现的最小的正整数。
O(n)
并且只使用常数级别额外空间的解决方案。
示例 1:
输入:nums = [1,2,0] 输出:3
示例 2:
输入:nums = [3,4,-1,1] 输出:2
示例 3:
输入:nums = [7,8,9,11,12] 输出:1
提示:
1 <= nums.length <= 5 * 105
-231 <= nums[i] <= 231 - 1
方法一:原地交换
我们假设数组 nums
长度为
遍历结束后,我们再遍历数组,如果
时间复杂度
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]
n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[i] != nums[nums[i] - 1]:
swap(i, nums[i] - 1)
for i in range(n):
if i + 1 != nums[i]:
return i + 1
return n + 1
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums, i, nums[i] - 1);
}
}
for (int i = 0; i < n; ++i) {
if (i + 1 != nums[i]) {
return i + 1;
}
}
return n + 1;
}
private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums[i], nums[nums[i] - 1]);
}
}
for (int i = 0; i < n; ++i) {
if (i + 1 != nums[i]) {
return i + 1;
}
}
return n + 1;
}
};
func firstMissingPositive(nums []int) int {
n := len(nums)
for i := range nums {
for nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i]-1] {
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
}
}
for i, v := range nums {
if i+1 != v {
return i + 1
}
}
return n + 1
}
int firstMissingPositive(int* nums, int numsSize) {
int Max = nums[0], i, *Count;
for (i = 1; i < numsSize; i++) {
Max = (Max < nums[i]) ? nums[i] : Max;
}
Count = (int*) calloc(Max + 1, sizeof(int));
for (i = 0; i < numsSize; i++) {
if (nums[i] > 0) {
Count[nums[i]]++;
}
}
i = 1;
while (Count[i] != 0) {
i++;
}
return i;
}
public class Solution {
public int FirstMissingPositive(int[] nums) {
var i = 0;
while (i < nums.Length)
{
if (nums[i] > 0 && nums[i] <= nums.Length)
{
var index = nums[i] -1;
if (index != i && nums[index] != nums[i])
{
var temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
}
else
{
++i;
}
}
else
{
++i;
}
}
for (i = 0; i < nums.Length; ++i)
{
if (nums[i] != i + 1)
{
return i + 1;
}
}
return nums.Length + 1;
}
}
function firstMissingPositive(nums: number[]): number {
const n = nums.length;
let i = 0;
while (i < n) {
const j = nums[i] - 1;
if (j === i || j < 0 || j >= n || nums[i] === nums[j]) {
i++;
} else {
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
const res = nums.findIndex((v, i) => v !== i + 1);
return (res === -1 ? n : res) + 1;
}
impl Solution {
pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut i = 0;
while i < n {
let j = nums[i] - 1;
if i as i32 == j || j < 0 || j >= n as i32 || nums[i] == nums[j as usize] {
i += 1;
} else {
nums.swap(i, j as usize);
}
}
nums.iter()
.enumerate()
.position(|(i, &v)| v as usize != i + 1)
.unwrap_or(n) as i32
+ 1
}
}