File tree Expand file tree Collapse file tree 2 files changed +16
-15
lines changed Expand file tree Collapse file tree 2 files changed +16
-15
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ public class FirstMissingPositive {
6
6
7
7
public int firstMissingPositive (int [] nums ) {
8
8
for (int i = 0 ; i < nums .length ; i ++) {
9
- while (nums [i ] >= 1 && nums [i ] <= nums .length && nums [nums [i ] - 1 ] != nums [i ]) {
9
+ while (nums [i ] - 1 >= 0 && nums [i ] - 1 < nums .length && nums [nums [i ] - 1 ] != nums [i ]) {
10
10
swap (nums , i , nums [i ] - 1 );
11
11
}
12
12
}
Original file line number Diff line number Diff line change @@ -4,24 +4,25 @@ public class Main {
4
4
5
5
public static class Solution {
6
6
7
- public int largestRectangleArea (int [] heights ) {
8
- int [] indexes = new int [heights .length + 1 ];
9
- int top = -1 , max = 0 ;
10
-
11
- for (int i = 0 ; i <= heights .length ; ) {
12
- int height = i == heights .length ? 0 : heights [i ];
13
- if (top < 0 || height > heights [indexes [top ]]) {
14
- indexes [++top ] = i ++;
15
- } else {
16
- int k = heights [indexes [top --]];
17
- int left = top < 0 ? 0 : (indexes [top ] + 1 );
18
- max = Math .max (max , (i - 1 - left + 1 ) * k );
7
+ public int firstMissingPositive (int [] nums ) {
8
+ for (int i = 0 ; i < nums .length ; i ++) {
9
+ while (nums [i ] - 1 < nums .length && nums [i ] - 1 >= 0 && nums [i ] != nums [nums [i ] - 1 ]) {
10
+ swap (nums , i , nums [i ] - 1 );
19
11
}
20
12
}
21
-
22
- return max ;
13
+ for (int i = 0 ; i < nums .length ; i ++) {
14
+ if (nums [i ] != i + 1 ) {
15
+ return i + 1 ;
16
+ }
17
+ }
18
+ return nums .length + 1 ;
23
19
}
24
20
21
+ private void swap (int [] nums , int i , int j ) {
22
+ int t = nums [i ];
23
+ nums [i ] = nums [j ];
24
+ nums [j ] = t ;
25
+ }
25
26
}
26
27
27
28
You can’t perform that action at this time.
0 commit comments