Skip to content

Commit 8ac117b

Browse files
committed
Pretty code
1 parent 8290bf1 commit 8ac117b

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

algorithms/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
| No. | Title | Go | <span id="Top">Difficulty</span> |
44
|-----| ----- | -- | ---------- |
5-
|0001|[两数之和](https://leetcode-cn.com/problems/two-sum/)|[go](./myarray/1.twoSum.go)|<font color="#009975">S</font>|
6-
|0002|[两数相加](https://leetcode-cn.com/problems/add-two-numbers/)|[go](./linkedList/2.addTwoNumbers.go)|<font color="#ed7336">M</font>|
7-
|0003|[无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)|[go](./mystring/3.lengthOfLongestSubstring.go)|<font color="#ed7336">M</font>|
8-
|0005|[最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/)|[go](./mystring/5.longestPalindrome.go)|<font color="#ed7336">M</font>|
5+
|0001|[两数之和](https://leetcode-cn.com/problems/two-sum/)|[go](./myarray/1.twoSum.go)|S|
6+
|0002|[两数相加](https://leetcode-cn.com/problems/add-two-numbers/)|[go](./linkedList/2.addTwoNumbers.go)|M|
7+
|0003|[无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)|[go](./mystring/3.lengthOfLongestSubstring.go)|M|
8+
|0005|[最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/)|[go](./mystring/5.longestPalindrome.go)|M|
99
|0007|[整数反转](https://leetcode-cn.com/problems/reverse-integer/)|[go](./mystring/7.reverse.go)|S|
10-
|0008|[字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi/)|[go](./mystring/8.myAtoi.go)|<font color="#ed7336">M</font>|
11-
|0011|[盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/submissions/)|[go](./myarray/11.maxArea.go)|<font color="red">M</font>|
10+
|0008|[字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi/)|[go](./mystring/8.myAtoi.go)|M|
11+
|0011|[盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/submissions/)|[go](./myarray/11.maxArea.go)|M|
1212
|0014|[最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/)|[go](./mystring/14.longestCommonPrefix.go)|S|
13-
|0015|[三数之和](https://leetcode-cn.com/problems/3sum/submissions/)|[go](./myarray/15.threeSum.go)|<font color="red">M</font>|
14-
|0019|[删除链表的倒数第N个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/)|[go](./linkedList/19.removeNthFromEnd.go)|<font color="red">M</font>|
13+
|0015|[三数之和](https://leetcode-cn.com/problems/3sum/submissions/)|[go](./myarray/15.threeSum.go)|M|
14+
|0019|[删除链表的倒数第N个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/)|[go](./linkedList/19.removeNthFromEnd.go)|M|
1515
|0020|[有效的括号](https://leetcode-cn.com/problems/valid-parentheses/)|[go](./mystring/20.isValid.go)|S|
1616
|0021|[合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/submissions/)|[go](./linkedList/21.mergeTwoSortedLists.go)|S|
17-
|0022|[括号生成](https://leetcode-cn.com/problems/generate-parentheses/)|[go](./tree/22.generateParenthesis.go)|<font color="red">M</font>|
17+
|0022|[括号生成](https://leetcode-cn.com/problems/generate-parentheses/)|[go](./tree/22.generateParenthesis.go)|M|
1818
|0026|[删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)|[go](./myarray/26.removeDuplicates.go)|S|
19-
|0032|[最长有效括号](https://leetcode-cn.com/problems/longest-valid-parentheses/)|[go](./mystring/32.longestValidParentheses.go)|<font color="#ec4c47">H</font>|
19+
|0032|[最长有效括号](https://leetcode-cn.com/problems/longest-valid-parentheses/)|[go](./mystring/32.longestValidParentheses.go)|M|
2020
|0033|[搜索旋转排序数组](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/submissions/)|[go](./myarray/33.search.go)|M|
2121
|0034|[在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)|[go](./myarray/34.searchRange.go)|M|
2222
|0053|[最大子序和](https://leetcode-cn.com/problems/maximum-subarray/)|[go](./myarray/53.maxSubArray.go)|S|

algorithms/tree/102.levelOrder.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package tree
22

3-
// 普通的BFS遍历
3+
// BFS 普通的BFS遍历
44
func BFS(root *TreeNode) []int { // {{{
55
var res []int
66
var queue []*TreeNode = []*TreeNode{root}
@@ -26,7 +26,7 @@ func BFS(root *TreeNode) []int { // {{{
2626

2727
// 1. 普通的BFS 比较简单 只需要什么一个[]int 一个存放tree的队列 利用queue先进先出的规则打印value即可
2828
// 2. 如果根据等级来保存[][]int的话 只要声明一个正在遍历的队列 一个保存下一层节点的队列即可
29-
func levelOrder(root *TreeNode) [][]int { // {{{
29+
func levelOrder1(root *TreeNode) [][]int { // {{{
3030
var res [][]int
3131
var queue []*TreeNode = []*TreeNode{root}
3232
if root == nil {
@@ -54,3 +54,25 @@ func levelOrder(root *TreeNode) [][]int { // {{{
5454
}
5555
return res
5656
} // }}}
57+
58+
func levelOrder2(root *TreeNode) [][]int {
59+
var res [][]int
60+
var dfs func(*TreeNode, int)
61+
62+
dfs = func(root *TreeNode, level int) {
63+
if root == nil {
64+
return
65+
}
66+
// 出现了新的level
67+
if level >= len(res) {
68+
res = append(res, []int{})
69+
}
70+
res[level] = append(res[level], root.Val)
71+
72+
dfs(root.Left, level+1)
73+
dfs(root.Right, level+1)
74+
}
75+
76+
dfs(root, 0)
77+
return res
78+
}
Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
package tree
22

33
import (
4-
_ "fmt"
54
"testing"
65

6+
"github.com/stretchr/testify/assert"
7+
78
"Leetcode/algorithms/kit"
89
)
910

1011
// go test -v base.go 102.*
1112
func Test_levelOrder(t *testing.T) {
12-
ints := []int{3, 9, 20, NULL, NULL, 15, 7}
13-
tests := kit.Ints2Tree(ints)
14-
t.Logf("case: %v\nBFS: %v\n levelOrder: %v\n",
15-
kit.BreadthFirstSearch(*tests), BFS(tests), levelOrder(tests))
13+
cases := []kit.CaseEntry{
14+
{
15+
Name: "x1",
16+
Input: []int{3, 9, 20, NULL, NULL, 15, 7},
17+
Expected: [][]int{
18+
[]int{3},
19+
[]int{9, 20},
20+
[]int{15, 7},
21+
},
22+
},
23+
{
24+
Name: "x2",
25+
Input: []int{3, 9, 20, 10, 11, 15, 7},
26+
Expected: [][]int{
27+
[]int{3},
28+
[]int{9, 20},
29+
[]int{10, 11, 15, 7},
30+
},
31+
},
32+
}
33+
34+
assert := assert.New(t)
35+
for _, c := range cases {
36+
t.Run(c.Name, func(t *testing.T) {
37+
root := kit.Ints2Tree(c.Input.([]int))
38+
assert.Equal(c.Expected, levelOrder1(root), "Input:%v", c.Input)
39+
assert.Equal(c.Expected, levelOrder2(root), "Input:%v", c.Input)
40+
})
41+
}
1642
}

algorithms/tree/104.maxDepth.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ func maxDepth(root *TreeNode) int {
1010
func max(a, b int) int {
1111
if a >= b {
1212
return a
13-
} else {
14-
return b
1513
}
14+
return b
1615
}

algorithms/tree/22.generateParenthesis.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package tree
22

3-
import _ "fmt"
4-
53
func generateParenthesis(n int) []string {
64
var res []string
75
if n == 0 {

algorithms/tree/base.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package tree
22

33
import "Leetcode/algorithms/kit"
44

5+
// NULL ...
56
var NULL = kit.NULL
67

8+
// TreeNode ...
79
type TreeNode = kit.TreeNode
10+
11+
// Stack ...
812
type Stack = kit.Stack
913

1014
// var TreeNode = kit.TreeNode

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
56
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
67
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
78
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

0 commit comments

Comments
 (0)