-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLesson 2 - MissingInteger.m
More file actions
40 lines (34 loc) · 897 Bytes
/
Lesson 2 - MissingInteger.m
File metadata and controls
40 lines (34 loc) · 897 Bytes
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
int solution(NSMutableArray *A) {
NSMutableArray * positive = [@[] mutableCopy];
int max = [A count];
int minPositiveValue = INT32_MAX;
for (int i = 0; i < [A count]; i++) {
int value = [A[i] intValue];
if (value > 0 && minPositiveValue > value) {
minPositiveValue = value;
}
}
if (minPositiveValue != 1) {
return 1;
}
for (int i = 0; i <= max; i++) {
[positive addObject:@0];
}
for (int i = 0; i < [A count]; i ++) {
int value = [A[i] intValue];
if (value >= 0 && value < [positive count]) {
positive[value] = @1;
}
}
for (int i = 1; i < [positive count]; i ++) {
int value = [positive[i] intValue];
if (value == 0) {
return i;
}
}
if (max > 0) {
return max + 1;
} else {
return 1;
}
}