Skip to content

Commit dc59df3

Browse files
aQuaaQua
aQua
authored and
aQua
committed
无法通过时长限制,在本机都无法通过。
1 parent 59725c1 commit dc59df3

File tree

5 files changed

+129
-17
lines changed

5 files changed

+129
-17
lines changed

Algorithms/0041.first-missing-positive/first-missing-positive.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
package Problem0041
22

3-
import (
4-
"fmt"
5-
)
6-
73
func firstMissingPositive(nums []int) int {
4+
// 整理 nums ,让 nums[k] == k+1,只要 k+1 存在于 nums 中
85
for i := 0; i < len(nums); i++ {
96

10-
fmt.Println(i)
7+
// fmt.Println(i)
118

129
for 0 <= nums[i]-1 && nums[i]-1 < len(nums) && nums[i] != nums[nums[i]-1] {
13-
// 0 <= nums[i]-1 && nums[i]-1 < len(nums) 是为了防止nums[nums[i]-1]溢出
10+
// 当 for 的判断语句成立时,
11+
// nums[i]-1 就是 k ,nums[i] 的值是 k+1
12+
// nums[i] != nums[nums[i]-1] 即是 k+1 != nums[k] ,这说明
13+
// 1. k+1 存在与 nums 中,
14+
// 2. k+1 还没有在他该在的 nums[k] 中
15+
// 通过互换,让 k+1 到 nums[k] 中去
1416
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
1517

16-
fmt.Println(nums)
18+
// fmt.Println(nums)
1719

20+
// 使用 for 而不是 if 是因为
21+
// nums[i] 中的新值,有可能是另一个 k+1 ,需要再让其归为
22+
// 如果使用 if ,而这个新的 k+1 又只有一个的话,
23+
// 这个新的 k+1 不会被处理到,不会被放在 nums[k] 中
1824
}
1925
}
26+
// 循环结束后,所有 1<=k+1<=len(nums) 且 k+1 存在于nums中,都会被存放于 nums[k] 中
2027

21-
for i := range nums {
22-
if nums[i] != i+1 {
23-
return i + 1
28+
// 整理后,第一个不存在的 k+1 就是答案
29+
for k := range nums {
30+
if nums[k] != k+1 {
31+
return k + 1
2432
}
2533
}
2634
return len(nums) + 1

Algorithms/0042.trapping-rain-water/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# [42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)
22

33
## 题目
4+
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
45

6+
For example,
7+
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
8+
![rainwatertrap](rainwatertrap.png)
9+
10+
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
511

612
## 解题思路
713

Loading
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,69 @@
11
package Problem0042
22

3+
func trap(height []int) int {
4+
height = trim(height)
5+
res := 0
6+
7+
for len(height) > 2 {
8+
for i := 1; i < len(height)-1; i++ {
9+
if hasHisherBefore(height, i) && hasHisherAfter(height, i) {
10+
height[i]++
11+
res++
12+
}
13+
}
14+
height = down(height)
15+
}
16+
17+
return res
18+
}
19+
20+
func hasHisherBefore(nums []int, index int) bool {
21+
for i := index - 1; i >= 0; i-- {
22+
if nums[i] > nums[index] {
23+
return true
24+
}
25+
}
26+
return false
27+
}
28+
func hasHisherAfter(nums []int, index int) bool {
29+
for i := index + 1; i < len(nums); i++ {
30+
if nums[i] > nums[index] {
31+
return true
32+
}
33+
}
34+
return false
35+
}
36+
37+
func trim(nums []int) []int {
38+
i := 0
39+
length := len(nums)
40+
for i < len(nums) {
41+
if nums[i] != 0 {
42+
nums = nums[i:]
43+
break
44+
}
45+
i++
46+
}
47+
48+
if i == length {
49+
return []int{}
50+
}
51+
52+
for i := len(nums) - 1; i >= 0; i-- {
53+
if nums[i] != 0 {
54+
nums = nums[:i+1]
55+
break
56+
}
57+
}
58+
return nums
59+
}
60+
61+
func down(nums []int) []int {
62+
for i := 0; i < len(nums); i++ {
63+
if nums[i] > 0 {
64+
nums[i]--
65+
}
66+
}
67+
68+
return trim(nums)
69+
}

Algorithms/0042.trapping-rain-water/trapping-rain-water_test.go

Lines changed: 38 additions & 7 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)