Skip to content

Commit f09ea6c

Browse files
halfrostdezhiy
authored andcommitted
Update README
1 parent 7d3ba34 commit f09ea6c

25 files changed

+2507
-2275
lines changed

README.md

+1,281-1,266
Large diffs are not rendered by default.

leetcode/0136.Single-Number/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ Your algorithm should have a linear runtime complexity. Could you implement it w
2626
## 解题思路
2727

2828
- 题目要求不能使用辅助空间,并且时间复杂度只能是线性的。
29-
- 题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现依次的数字,因为那些出现两次的数字全部在异或中抵消掉了。于是最终做法是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。**利用的性质是 x^x = 0**
29+
- 题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现一次的数字,因为那些出现两次的数字全部在异或中抵消掉了。于是最终做法是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。**利用的性质是 x^x = 0**

leetcode/0301.Remove-Invalid-Parentheses/README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [301. Remove Invalid Parentheses](https://leetcode-cn.com/problems/remove-invalid-parentheses/)
1+
# [301. Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)
22

33

44
## 题目
@@ -53,3 +53,79 @@ Return all the possible results. You may return the answer in any order.
5353
- 得分小于0
5454
- lmoves小于0
5555
- rmoves小于0
56+
57+
## 代码
58+
59+
```go
60+
61+
package leetcode
62+
63+
var (
64+
res []string
65+
mp map[string]int
66+
n int
67+
length int
68+
maxScore int
69+
str string
70+
)
71+
72+
func removeInvalidParentheses(s string) []string {
73+
lmoves, rmoves, lcnt, rcnt := 0, 0, 0, 0
74+
for _, v := range s {
75+
if v == '(' {
76+
lmoves++
77+
lcnt++
78+
} else if v == ')' {
79+
if lmoves != 0 {
80+
lmoves--
81+
} else {
82+
rmoves++
83+
}
84+
rcnt++
85+
}
86+
}
87+
n = len(s)
88+
length = n - lmoves - rmoves
89+
res = []string{}
90+
mp = make(map[string]int)
91+
maxScore = min(lcnt, rcnt)
92+
str = s
93+
backtrace(0, "", lmoves, rmoves, 0)
94+
return res
95+
}
96+
97+
func backtrace(i int, cur string, lmoves int, rmoves int, score int) {
98+
if lmoves < 0 || rmoves < 0 || score < 0 || score > maxScore {
99+
return
100+
}
101+
if lmoves == 0 && rmoves == 0 {
102+
if len(cur) == length {
103+
if _, ok := mp[cur]; !ok {
104+
res = append(res, cur)
105+
mp[cur] = 1
106+
}
107+
return
108+
}
109+
}
110+
if i == n {
111+
return
112+
}
113+
if str[i] == '(' {
114+
backtrace(i+1, cur+string('('), lmoves, rmoves, score+1)
115+
backtrace(i+1, cur, lmoves-1, rmoves, score)
116+
} else if str[i] == ')' {
117+
backtrace(i+1, cur+string(')'), lmoves, rmoves, score-1)
118+
backtrace(i+1, cur, lmoves, rmoves-1, score)
119+
} else {
120+
backtrace(i+1, cur+string(str[i]), lmoves, rmoves, score)
121+
}
122+
}
123+
124+
func min(a, b int) int {
125+
if a < b {
126+
return a
127+
}
128+
return b
129+
}
130+
131+
```

website/content/ChapterFour/0300~0399/0300.Longest-Increasing-Subsequence.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ func lengthOfLIS1(nums []int) int {
8383
----------------------------------------------
8484
<div style="display: flex;justify-content: space-between;align-items: center;">
8585
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0297.Serialize-and-Deserialize-Binary-Tree/">⬅️上一页</a></p>
86-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0303.Range-Sum-Query-Immutable/">下一页➡️</a></p>
86+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0301.Remove-Invalid-Parentheses/">下一页➡️</a></p>
8787
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# [301. Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)
2+
3+
4+
## 题目
5+
6+
Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid.
7+
8+
Return all the possible results. You may return the answer in any order.
9+
10+
**Example 1:**
11+
12+
Input: s = "()())()"
13+
Output: ["(())()","()()()"]
14+
15+
**Example 2:**
16+
17+
Input: s = "(a)())()"
18+
Output: ["(a())()","(a)()()"]
19+
20+
**Example 3:**
21+
22+
Input: s = ")("
23+
Output: [""]
24+
25+
**Constraints:**
26+
27+
- 1 <= s.length <= 25
28+
- s consists of lowercase English letters and parentheses '(' and ')'.
29+
- There will be at most 20 parentheses in s.
30+
31+
## 题目大意
32+
33+
给你一个由若干括号和字母组成的字符串 s ,删除最小数量的无效括号,使得输入的字符串有效。
34+
35+
返回所有可能的结果。答案可以按 任意顺序 返回。
36+
37+
说明:
38+
39+
- 1 <= s.length <= 25
40+
- s 由小写英文字母以及括号 '(' 和 ')' 组成
41+
- s 中至多含 20 个括号
42+
43+
44+
## 解题思路
45+
46+
回溯和剪枝
47+
- 计算最大得分数maxScore,合法字符串的长度length,左括号和右括号的移除次数lmoves,rmoves
48+
- 加一个左括号的得分加1;加一个右括号的得分减1
49+
- 对于一个合法的字符串,左括号等于右括号,得分最终为0;
50+
- 搜索过程中出现以下任何一种情况都直接返回
51+
- 得分值为负数
52+
- 得分大于最大得分数
53+
- 得分小于0
54+
- lmoves小于0
55+
- rmoves小于0
56+
57+
## 代码
58+
59+
```go
60+
61+
package leetcode
62+
63+
var (
64+
res []string
65+
mp map[string]int
66+
n int
67+
length int
68+
maxScore int
69+
str string
70+
)
71+
72+
func removeInvalidParentheses(s string) []string {
73+
lmoves, rmoves, lcnt, rcnt := 0, 0, 0, 0
74+
for _, v := range s {
75+
if v == '(' {
76+
lmoves++
77+
lcnt++
78+
} else if v == ')' {
79+
if lmoves != 0 {
80+
lmoves--
81+
} else {
82+
rmoves++
83+
}
84+
rcnt++
85+
}
86+
}
87+
n = len(s)
88+
length = n - lmoves - rmoves
89+
res = []string{}
90+
mp = make(map[string]int)
91+
maxScore = min(lcnt, rcnt)
92+
str = s
93+
backtrace(0, "", lmoves, rmoves, 0)
94+
return res
95+
}
96+
97+
func backtrace(i int, cur string, lmoves int, rmoves int, score int) {
98+
if lmoves < 0 || rmoves < 0 || score < 0 || score > maxScore {
99+
return
100+
}
101+
if lmoves == 0 && rmoves == 0 {
102+
if len(cur) == length {
103+
if _, ok := mp[cur]; !ok {
104+
res = append(res, cur)
105+
mp[cur] = 1
106+
}
107+
return
108+
}
109+
}
110+
if i == n {
111+
return
112+
}
113+
if str[i] == '(' {
114+
backtrace(i+1, cur+string('('), lmoves, rmoves, score+1)
115+
backtrace(i+1, cur, lmoves-1, rmoves, score)
116+
} else if str[i] == ')' {
117+
backtrace(i+1, cur+string(')'), lmoves, rmoves, score-1)
118+
backtrace(i+1, cur, lmoves, rmoves-1, score)
119+
} else {
120+
backtrace(i+1, cur+string(str[i]), lmoves, rmoves, score)
121+
}
122+
}
123+
124+
func min(a, b int) int {
125+
if a < b {
126+
return a
127+
}
128+
return b
129+
}
130+
131+
```
132+
133+
134+
----------------------------------------------
135+
<div style="display: flex;justify-content: space-between;align-items: center;">
136+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0300.Longest-Increasing-Subsequence/">⬅️上一页</a></p>
137+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0303.Range-Sum-Query-Immutable/">下一页➡️</a></p>
138+
</div>

website/content/ChapterFour/0300~0399/0303.Range-Sum-Query-Immutable.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ func (ma *NumArray) SumRange(i int, j int) int {
112112

113113
----------------------------------------------
114114
<div style="display: flex;justify-content: space-between;align-items: center;">
115-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0300.Longest-Increasing-Subsequence/">⬅️上一页</a></p>
115+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0301.Remove-Invalid-Parentheses/">⬅️上一页</a></p>
116116
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0304.Range-Sum-Query-2D-Immutable/">下一页➡️</a></p>
117117
</div>

0 commit comments

Comments
 (0)