-
Notifications
You must be signed in to change notification settings - Fork 30
/
FirstMissingPositive.cs
executable file
·60 lines (57 loc) · 2.89 KB
/
FirstMissingPositive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Source : https://leetcode.com/problems/first-missing-positive/
// Author : codeyu
// Date : Tuesday, October 25, 2016 11:16:10 PM
/**********************************************************************************
*
*
* 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.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution041
{
public static int FirstMissingPositive(int[] nums)
{
for(int i = 0; i < nums.Length;)
{
if(nums[i] > 0 && nums[i] <= nums.Length && nums[nums[i] - 1] != nums[i])
{
Swap(ref nums[i], ref nums[nums[i] - 1]);
}
else
{
i++;
}
}
for(int i = 0; i < nums.Length; i++)
{
if(nums[i] != i + 1)
{
return i + 1;
}
}
return nums.Length + 1;
}
private static void Swap<T> (ref T lhs, ref T rhs)
{
T temp = lhs;
lhs = rhs;
rhs = temp;
}
}
}