- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
43 lines (37 loc) · 758 Bytes
/
index.ts
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
/**
# 41. First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.
## Example
```bash
Input: [1,2,0]
Output: 3
```
```bash
Input: [3,4,-1,1]
Output: 2
```
```bash
Input: [7,8,9,11,12]
Output: 1
```
## Note
Your algorithm should run in O(n) time and uses constant extra space.
*/
exporttypeSolution=(nums: number[])=>number;
/**
* 粗暴解法
* @author yalda
* @github https://github.com/guocaoyi/leetcode-ts
*/
exportconstfirstMissingPositive=(nums: number[]): number=>{
letcounter=1;
nums.sort((pre: number,next: number)=>pre-next);
for(leti=0;i<nums.length;i++){
if(nums[i]>counter){
counter++;
}else{
break;
}
}
returncounter;
};