Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5391ea4

Browse files
committed
Add solution 41.
1 parent 1c62ef1 commit 5391ea4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
3+
/**
4+
* @param Integer[] $nums
5+
* @return Integer
6+
*/
7+
function firstMissingPositive($nums) {
8+
if(count($nums) == 0) return 1;
9+
for($i = 0;$i < count($nums);$i++) {
10+
while(($nums[$i] != $i + 1) && $nums[$i] >= 1 && $nums[$i] <= count($nums) && $nums[$nums[$i] - 1] != $nums[$i]) {
11+
$tmp = $nums[$i] - 1;
12+
$nums[$i] = $nums[$tmp];
13+
$nums[$tmp] = $tmp + 1;
14+
}
15+
}
16+
for($i = 0;$i < count($nums);$i++) {
17+
if($nums[$i] != $i + 1) return $i + 1;
18+
}
19+
return count($nums) + 1;
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /