Skip to content

Commit d5c5284

Browse files
committed
Update solution 0617 & Add solution 0752
1 parent bc7c912 commit d5c5284

18 files changed

+553
-33
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@
126126

127127
| | Easy | Medium | Hard | Total |
128128
|:--------:|:--------:|:--------:|:--------:|:--------:|
129-
|Optimizing|33|31|23|87|
130-
|Accepted|**284**|**392**|**115**|**791**|
129+
|Optimizing|33|30|23|86|
130+
|Accepted|**285**|**392**|**115**|**792**|
131131
|Total|501|1010|400|1911|
132-
|Perfection Rate|88.4%|92.1%|80.0%|89.0%|
133-
|Completion Rate|56.7%|38.8%|28.7%|41.4%|
132+
|Perfection Rate|88.4%|92.3%|80.0%|89.1%|
133+
|Completion Rate|56.9%|38.8%|28.7%|41.4%|
134134
|------------|----------------------------|----------------------------|----------------------------|----------------------------|
135135

136136
## 二. 目录
137137

138-
以下已经收录了 704 道题的题解,还有 11 道题在尝试优化到 beats 100%
138+
以下已经收录了 706 道题的题解,还有 11 道题在尝试优化到 beats 100%
139139

140140
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
141141
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
@@ -335,7 +335,7 @@
335335
|0194|Transpose File||24.5%|Medium||
336336
|0195|Tenth Line||32.7%|Easy||
337337
|0196|Delete Duplicate Emails||47.2%|Easy||
338-
|0197|Rising Temperature||40.7%|Easy||
338+
|0197|Rising Temperature||40.6%|Easy||
339339
|0198|House Robber|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0198.House-Robber)|43.7%|Medium||
340340
|0199|Binary Tree Right Side View|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0199.Binary-Tree-Right-Side-View)|57.1%|Medium||
341341
|0200|Number of Islands|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0200.Number-of-Islands)|50.4%|Medium||
@@ -755,7 +755,7 @@
755755
|0614|Second Degree Follower||33.3%|Medium||
756756
|0615|Average Salary: Departments VS Company||53.9%|Hard||
757757
|0616|Add Bold Tag in String||45.3%|Medium||
758-
|0617|Merge Two Binary Trees||76.0%|Easy||
758+
|0617|Merge Two Binary Trees|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0617.Merge-Two-Binary-Trees)|76.0%|Easy||
759759
|0618|Students Report By Geography||61.5%|Hard||
760760
|0619|Biggest Single Number||45.8%|Easy||
761761
|0620|Not Boring Movies||70.8%|Easy||
@@ -890,7 +890,7 @@
890890
|0749|Contain Virus||49.0%|Hard||
891891
|0750|Number Of Corner Rectangles||67.3%|Medium||
892892
|0751|IP to CIDR||58.2%|Medium||
893-
|0752|Open the Lock||54.7%|Medium||
893+
|0752|Open the Lock|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0752.Open-the-Lock)|54.7%|Medium||
894894
|0753|Cracking the Safe|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0753.Cracking-the-Safe)|52.9%|Hard||
895895
|0754|Reach a Number||40.7%|Medium||
896896
|0755|Pour Water||44.5%|Medium||
@@ -1494,7 +1494,7 @@
14941494
|1354|Construct Target Array With Multiple Sums||31.4%|Hard||
14951495
|1355|Activity Participants||74.7%|Medium||
14961496
|1356|Sort Integers by The Number of 1 Bits||70.6%|Easy||
1497-
|1357|Apply Discount Every n Orders||67.2%|Medium||
1497+
|1357|Apply Discount Every n Orders||67.3%|Medium||
14981498
|1358|Number of Substrings Containing All Three Characters||61.0%|Medium||
14991499
|1359|Count All Valid Pickup and Delivery Options||55.7%|Hard||
15001500
|1360|Number of Days Between Two Dates||46.5%|Easy||
@@ -1706,7 +1706,7 @@
17061706
|1566|Detect Pattern of Length M Repeated K or More Times||43.0%|Easy||
17071707
|1567|Maximum Length of Subarray With Positive Product||37.6%|Medium||
17081708
|1568|Minimum Number of Days to Disconnect Island||50.1%|Hard||
1709-
|1569|Number of Ways to Reorder Array to Get Same BST||50.1%|Hard||
1709+
|1569|Number of Ways to Reorder Array to Get Same BST||50.0%|Hard||
17101710
|1570|Dot Product of Two Sparse Vectors||91.1%|Medium||
17111711
|1571|Warehouse Manager||89.6%|Easy||
17121712
|1572|Matrix Diagonal Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1572.Matrix-Diagonal-Sum)|78.0%|Easy||
@@ -1911,7 +1911,7 @@
19111911
|1771|Maximize Palindrome Length From Subsequences||34.4%|Hard||
19121912
|1772|Sort Features by Popularity||65.5%|Medium||
19131913
|1773|Count Items Matching a Rule||84.6%|Easy||
1914-
|1774|Closest Dessert Cost||45.4%|Medium||
1914+
|1774|Closest Dessert Cost||45.3%|Medium||
19151915
|1775|Equal Sum Arrays With Minimum Number of Operations||50.5%|Medium||
19161916
|1776|Car Fleet II||49.9%|Hard||
19171917
|1777|Product's Price for Each Store||86.3%|Easy||
@@ -2022,7 +2022,7 @@
20222022
|1882|Process Tasks Using Servers||30.6%|Medium||
20232023
|1883|Minimum Skips to Arrive at Meeting On Time||38.2%|Hard||
20242024
|1884|Egg Drop With 2 Eggs and N Floors||69.9%|Medium||
2025-
|1885|Count Pairs in Two Arrays||54.8%|Medium||
2025+
|1885|Count Pairs in Two Arrays||54.9%|Medium||
20262026
|1886|Determine Whether Matrix Can Be Obtained By Rotation||53.9%|Easy||
20272027
|1887|Reduction Operations to Make the Array Elements Equal||59.7%|Medium||
20282028
|1888|Minimum Number of Flips to Make the Binary String Alternating||33.8%|Medium||
@@ -2033,7 +2033,7 @@
20332033
|1893|Check if All the Integers in a Range Are Covered||49.1%|Easy||
20342034
|1894|Find the Student that Will Replace the Chalk||38.6%|Medium||
20352035
|1895|Largest Magic Square||49.3%|Medium||
2036-
|1896|Minimum Cost to Change the Final Value of Expression||51.5%|Hard||
2036+
|1896|Minimum Cost to Change the Final Value of Expression||51.4%|Hard||
20372037
|1897|Redistribute Characters to Make All Strings Equal||58.6%|Easy||
20382038
|1898|Maximum Number of Removable Characters||32.3%|Medium||
20392039
|1899|Merge Triplets to Form Target Triplet||58.9%|Medium||
@@ -2044,12 +2044,12 @@
20442044
|1904|The Number of Full Rounds You Have Played||52.6%|Medium||
20452045
|1905|Count Sub Islands||59.9%|Medium||
20462046
|1906|Minimum Absolute Difference Queries||41.5%|Medium||
2047-
|1907|Count Salary Categories||62.1%|Medium||
2047+
|1907|Count Salary Categories||62.2%|Medium||
20482048
|1908|Game of Nim||66.8%|Medium||
2049-
|1909|Remove One Element to Make the Array Strictly Increasing||28.4%|Easy||
2050-
|1910|Remove All Occurrences of a Substring||70.7%|Medium||
2051-
|1911|Maximum Alternating Subsequence Sum||52.5%|Medium||
2052-
|1912|Design Movie Rental System||33.8%|Hard||
2049+
|1909|Remove One Element to Make the Array Strictly Increasing||28.5%|Easy||
2050+
|1910|Remove All Occurrences of a Substring||70.8%|Medium||
2051+
|1911|Maximum Alternating Subsequence Sum||52.6%|Medium||
2052+
|1912|Design Movie Rental System||33.9%|Hard||
20532053
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|
20542054

20552055
------------------------------------------------------------------

leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
2020
if root1 == nil {
2121
return root2
2222
}
23-
2423
if root2 == nil {
2524
return root1
2625
}
27-
2826
root1.Val += root2.Val
2927
root1.Left = mergeTrees(root1.Left, root2.Left)
3028
root1.Right = mergeTrees(root1.Right, root2.Right)
31-
3229
return root1
3330
}
Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,80 @@
11
# [617. Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)
22

3+
34
## 题目
45

5-
You are given two binary trees **root1** and **root2**.
6+
You are given two binary trees `root1` and `root2`.
67

78
Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
89

9-
Return the merged tree.
10+
Return *the merged tree*.
11+
12+
**Note:** The merging process must start from the root nodes of both trees.
1013

11-
**Note**: The merging process must start from the root nodes of both trees.
14+
**Example 1:**
1215

13-
**Example 1**:
16+
![https://assets.leetcode.com/uploads/2021/02/05/merge.jpg](https://assets.leetcode.com/uploads/2021/02/05/merge.jpg)
1417

1518
```
1619
Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
1720
Output: [3,4,5,5,4,null,7]
1821
```
1922

20-
**Example 2**:
23+
**Example 2:**
2124

2225
```
2326
Input: root1 = [1], root2 = [1,2]
2427
Output: [2,2]
2528
```
2629

27-
**Constraints**:
30+
**Constraints:**
31+
32+
- The number of nodes in both trees is in the range `[0, 2000]`.
33+
- `104 <= Node.val <= 104`
34+
35+
## 题目大意
36+
37+
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
38+
39+
## 解题思路
40+
41+
- 简单题。采用深搜的思路,分别从根节点开始同时遍历两个二叉树,并将对应的节点进行合并。两个二叉树的对应节点可能存在以下三种情况:
42+
- 如果两个二叉树的对应节点都为空,则合并后的二叉树的对应节点也为空;
43+
- 如果两个二叉树的对应节点只有一个为空,则合并后的二叉树的对应节点为其中的非空节点;
44+
- 如果两个二叉树的对应节点都不为空,则合并后的二叉树的对应节点的值为两个二叉树的对应节点的值之和,此时需要显性合并两个节点。
45+
- 对一个节点进行合并之后,还要对该节点的左右子树分别进行合并。用递归实现即可。
46+
47+
## 代码
48+
49+
```go
50+
package leetcode
51+
52+
import (
53+
"github.com/halfrost/LeetCode-Go/structures"
54+
)
55+
56+
// TreeNode define
57+
type TreeNode = structures.TreeNode
58+
59+
/**
60+
* Definition for a binary tree node.
61+
* type TreeNode struct {
62+
* Val int
63+
* Left *TreeNode
64+
* Right *TreeNode
65+
* }
66+
*/
2867

29-
1. The number of nodes in both trees is in the range [0, 2000].
30-
2. -104 <= Node.val <= 104
68+
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
69+
if root1 == nil {
70+
return root2
71+
}
72+
if root2 == nil {
73+
return root1
74+
}
75+
root1.Val += root2.Val
76+
root1.Left = mergeTrees(root1.Left, root2.Left)
77+
root1.Right = mergeTrees(root1.Right, root2.Right)
78+
return root1
79+
}
80+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
func openLock(deadends []string, target string) int {
4+
if target == "0000" {
5+
return 0
6+
}
7+
targetNum, visited := strToInt(target), make([]bool, 10000)
8+
visited[0] = true
9+
for _, deadend := range deadends {
10+
num := strToInt(deadend)
11+
if num == 0 {
12+
return -1
13+
}
14+
visited[num] = true
15+
}
16+
depth, curDepth, nextDepth := 0, []int16{0}, make([]int16, 0)
17+
var nextNum int16
18+
for len(curDepth) > 0 {
19+
nextDepth = nextDepth[0:0]
20+
for _, curNum := range curDepth {
21+
for incrementer := int16(1000); incrementer > 0; incrementer /= 10 {
22+
digit := (curNum / incrementer) % 10
23+
if digit == 9 {
24+
nextNum = curNum - 9*incrementer
25+
} else {
26+
nextNum = curNum + incrementer
27+
}
28+
if nextNum == targetNum {
29+
return depth + 1
30+
}
31+
if !visited[nextNum] {
32+
visited[nextNum] = true
33+
nextDepth = append(nextDepth, nextNum)
34+
}
35+
if digit == 0 {
36+
nextNum = curNum + 9*incrementer
37+
} else {
38+
nextNum = curNum - incrementer
39+
}
40+
if nextNum == targetNum {
41+
return depth + 1
42+
}
43+
if !visited[nextNum] {
44+
visited[nextNum] = true
45+
nextDepth = append(nextDepth, nextNum)
46+
}
47+
}
48+
}
49+
curDepth, nextDepth = nextDepth, curDepth
50+
depth++
51+
}
52+
return -1
53+
}
54+
55+
func strToInt(str string) int16 {
56+
return int16(str[0]-'0')*1000 + int16(str[1]-'0')*100 + int16(str[2]-'0')*10 + int16(str[3]-'0')
57+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question752 struct {
9+
para752
10+
ans752
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para752 struct {
16+
deadends []string
17+
target string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans752 struct {
23+
one int
24+
}
25+
26+
func Test_Problem752(t *testing.T) {
27+
28+
qs := []question752{
29+
30+
{
31+
para752{[]string{"0201", "0101", "0102", "1212", "2002"}, "0202"},
32+
ans752{6},
33+
},
34+
35+
{
36+
para752{[]string{"8888"}, "0009"},
37+
ans752{1},
38+
},
39+
40+
{
41+
para752{[]string{"8887", "8889", "8878", "8898", "8788", "8988", "7888", "9888"}, "8888"},
42+
ans752{-1},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 752------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans752, q.para752
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, openLock(p.deadends, p.target))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}

0 commit comments

Comments
 (0)