Skip to content

Commit e246e20

Browse files
committed
update
1 parent e2a010c commit e246e20

21 files changed

+242
-164
lines changed

100.相同的树.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
* }
1515
*/
1616
func isSameTree(p *TreeNode, q *TreeNode) bool {
17+
// 全部为nil
1718
if p == nil && q == nil {
1819
return true
1920
}
21+
// 其中一个为nil
2022
if p == nil || q == nil {
2123
return false
2224
}

102.二叉树的层序遍历.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ func levelOrder(root *TreeNode) [][]int {
6565
for i := 0; i < size; i++ {
6666
// get node
6767
node := queue[i]
68-
// 如果没做nil值过滤, 这里需要加
69-
/*
70-
if node == nil {
71-
continue
72-
}
73-
*/
68+
7469
// append value
7570
levelData = append(levelData, node.Val)
7671

@@ -82,7 +77,6 @@ func levelOrder(root *TreeNode) [][]int {
8277
if node.Right != nil {
8378
queue = append(queue, node.Right)
8479
}
85-
// queue = append(queue, node.Left, node.Right)
8680
}
8781
// 最后整体pop
8882
queue = queue[size:]

111.二叉树的最小深度.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ func minDepth(root *TreeNode) int {
4545
return count
4646
}
4747

48-
func min(x, y int) int {
49-
if x < y {
50-
return x
51-
}
52-
return y
53-
}
54-
5548
// @lc code=end
5649

5750
// 深度优先
@@ -70,4 +63,11 @@ func minDepth(root *TreeNode) int {
7063
minD = min(minDepth(root.Right), minD)
7164
}
7265
return minD + 1
66+
}
67+
68+
func min(x, y int) int {
69+
if x < y {
70+
return x
71+
}
72+
return y
7373
}

112.路径总和.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ func hasPathSum(root *TreeNode, targetSum int) bool {
1717
if root == nil {
1818
return false
1919
}
20-
return has(root, targetSum, 0)
21-
}
22-
23-
func has(node *TreeNode, targetSum, parentSum int) bool {
24-
curVal := parentSum + node.Val
20+
var has func(*TreeNode, int) bool
21+
has = func(node *TreeNode, parentSum int) bool {
22+
curVal := parentSum + node.Val
2523

26-
if node.Left == nil && node.Right == nil {
27-
return curVal == targetSum
28-
}
24+
if node.Left == nil && node.Right == nil {
25+
return curVal == targetSum
26+
}
2927

30-
leftHas, rightHas := false, false
31-
if node.Left != nil {
32-
leftHas = has(node.Left, targetSum, curVal)
33-
}
34-
if node.Right != nil {
35-
rightHas = has(node.Right, targetSum, curVal)
28+
leftHas, rightHas := false, false
29+
if node.Left != nil {
30+
leftHas = has(node.Left, curVal)
31+
}
32+
if node.Right != nil {
33+
rightHas = has(node.Right, curVal)
34+
}
35+
return leftHas || rightHas
3636
}
37-
return leftHas || rightHas
37+
return has(root, 0)
3838
}
3939

4040
// @lc code=end

141.环形链表.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@ func hasCycle(head *ListNode) bool {
2020
}
2121

2222
slow := head
23-
fast := head.Next
23+
fast := head
2424

25-
for slow != fast {
25+
for fast != nil {
2626
// 因为fast肯定最先达到nil, 因此可以不判断slow
27-
//if slow == nil || fast == nil || fast.Next == nil {
28-
if fast == nil || fast.Next == nil {
27+
// fast已经到达了末尾, 说明无环
28+
if fast.Next == nil {
2929
return false
3030
}
3131
slow = slow.Next
3232
fast = fast.Next.Next
33-
}
34-
return true
3533

34+
if fast == slow {
35+
return true
36+
}
37+
}
38+
return false
3639
}
3740

3841
// @lc code=end

146.lru-缓存.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ type LRUCache struct {
3838
Cache map[int]*Node
3939
}
4040

41+
/*
42+
1. hashtable缓存
43+
2. 双向链表
44+
3. 最新访问过的, 放在tail
45+
4. 超出capacity, 删掉head
46+
*/
4147
func Constructor(capacity int) LRUCache {
4248
head := NewNode(-1, 0)
4349
tail := NewNode(-100, 0)
@@ -60,16 +66,6 @@ func NewNode(key, value int) *Node {
6066
}
6167
}
6268

63-
// 如果不存在返回-1, 存在需要将其放在tail
64-
func (this *LRUCache) Get(key int) int {
65-
n, ok := this.Cache[key]
66-
if !ok {
67-
return -1
68-
}
69-
this.move2Tail(n)
70-
return n.Value
71-
}
72-
7369
// 首先将其从原位置删除, 然后添加到tail
7470
func (this *LRUCache) move2Tail(n *Node) {
7571
this.removeNode(n)
@@ -91,6 +87,16 @@ func (this *LRUCache) removeNode(n *Node) {
9187
n.Next.Pre = n.Pre
9288
}
9389

90+
// 如果不存在返回-1, 存在需要将其放在tail
91+
func (this *LRUCache) Get(key int) int {
92+
n, ok := this.Cache[key]
93+
if !ok {
94+
return -1
95+
}
96+
this.move2Tail(n)
97+
return n.Value
98+
}
99+
94100
func (this *LRUCache) Put(key int, value int) {
95101
// 如果已存在, 需要更新其value, 然后移动到tail
96102
if n, ok := this.Cache[key]; ok {

160.相交链表.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
3434
}
3535
p1, p2 := headA, headB
3636

37-
//最差的情况, 两个指针都走了m+n个node,
38-
// 相交部分为c
39-
// 1. 如果不相交, 两者均为nil
40-
// 2. 如果相交, 两者会在第一个交点相遇
37+
//
38+
// 1. 如果不相交, 两个指针都走了m+n个node, 最终均为nil
39+
// 2. 如果相交, 两者会在第一个交点相遇, 相交部分为c, 则二者均走过m+n-c
4140
for p1 != p2 {
4241
if p1 == nil {
4342
p1 = headB

191.位-1-的个数.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package main
2-
31
/*
42
* @lc app=leetcode.cn id=191 lang=golang
53
*
@@ -11,11 +9,9 @@ package main
119
func hammingWeight(num uint32) int {
1210
count := 0
1311

14-
for i := 0; i < 32; i++ {
15-
// 第i位为1, 结果才大于0
16-
if 1<<i&num > 0 {
17-
count++
18-
}
12+
for num > 0 {
13+
num &= (num -1)
14+
count++
1915
}
2016
return count
2117
}

26.删除有序数组中的重复项.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66

77
// @lc code=start
88
func removeDuplicates(nums []int) int {
9-
size := len(nums)
10-
if size < 2 {
11-
return size
12-
}
13-
left := 1
14-
for right := 1; right < size; right++ {
15-
if nums[right] != nums[right-1] {
16-
// 直接用right值覆盖left
17-
nums[left] = nums[right]
18-
left++
9+
left, right := 0, 1
10+
11+
for right < len(nums) {
12+
if nums[left] != nums[right] {
13+
if left != right {
14+
nums[left+1] = nums[right]
15+
left++
16+
} else {
17+
left++
18+
}
1919
}
20+
21+
right++
22+
2023
}
21-
return left
24+
// need size not index
25+
return left + 1
2226

2327
}
2428

27.移除元素.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
// 双指针法, 将等于val的值全部移动到右面
99
func removeElement(nums []int, val int) int {
1010

11-
// 存储第一个值为val的index
12-
slowIdx := 0
11+
left, right := 0, 0
1312

14-
for fastIdx := 0; fastIdx < len(nums); fastIdx++ {
15-
if nums[fastIdx] != val {
16-
if slowIdx != fastIdx {
17-
swap(nums, slowIdx, fastIdx)
13+
for right < len(nums) {
14+
if nums[right] != val {
15+
if left == right {
16+
left++
17+
} else {
18+
swap(nums, left, right)
19+
left++
1820
}
19-
// 可能连续多个item都为val, 因此只能加1
20-
slowIdx++
2121
}
22+
right++
2223
}
23-
nums = nums[:slowIdx]
24-
return slowIdx
24+
return left
2525
}
2626

2727
func swap(arr []int, i, j int) {

0 commit comments

Comments
 (0)