Skip to content

Commit d53f294

Browse files
committed
fd
1 parent 589b5bc commit d53f294

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

leetcode/solution/src/FirstMissingPositive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class FirstMissingPositive {
66

77
public int firstMissingPositive(int[] nums) {
88
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]) {
1010
swap(nums, i, nums[i] - 1);
1111
}
1212
}

leetcode/src/Main.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ public class Main {
44

55
public static class Solution {
66

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);
1911
}
2012
}
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;
2319
}
2420

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+
}
2526
}
2627

2728

0 commit comments

Comments
 (0)