Skip to content

Commit 7d08f48

Browse files
committed
add: leetcode 0301 solution
1 parent af654b2 commit 7d08f48

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package leetcode
2+
3+
var (
4+
res []string
5+
mp map[string]int
6+
n int
7+
length int
8+
maxScore int
9+
str string
10+
)
11+
12+
func removeInvalidParentheses(s string) []string {
13+
lmoves, rmoves, lcnt, rcnt := 0, 0, 0, 0
14+
for _, v := range s {
15+
if v == '(' {
16+
lmoves++
17+
lcnt++
18+
} else if v == ')' {
19+
if lmoves != 0 {
20+
lmoves--
21+
} else {
22+
rmoves++
23+
}
24+
rcnt++
25+
}
26+
}
27+
n = len(s)
28+
length = n - lmoves - rmoves
29+
res = []string{}
30+
mp = make(map[string]int)
31+
maxScore = min(lcnt, rcnt)
32+
str = s
33+
backtrace(0, "", lmoves, rmoves, 0)
34+
return res
35+
}
36+
37+
func backtrace(i int, cur string, lmoves int, rmoves int, score int) {
38+
if lmoves < 0 || rmoves < 0 || score < 0 || score > maxScore {
39+
return
40+
}
41+
if lmoves == 0 && rmoves == 0 {
42+
if len(cur) == length {
43+
if _, ok := mp[cur]; !ok {
44+
res = append(res, cur)
45+
mp[cur] = 1
46+
}
47+
return
48+
}
49+
}
50+
if i == n {
51+
return
52+
}
53+
if str[i] == '(' {
54+
backtrace(i+1, cur+string('('), lmoves, rmoves, score+1)
55+
backtrace(i+1, cur, lmoves-1, rmoves, score)
56+
} else if str[i] == ')' {
57+
backtrace(i+1, cur+string(')'), lmoves, rmoves, score-1)
58+
backtrace(i+1, cur, lmoves, rmoves-1, score)
59+
} else {
60+
backtrace(i+1, cur+string(str[i]), lmoves, rmoves, score)
61+
}
62+
}
63+
64+
func min(a, b int) int {
65+
if a < b {
66+
return a
67+
}
68+
return b
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question301 struct {
9+
para301
10+
ans301
11+
}
12+
13+
// s 是参数
14+
type para301 struct {
15+
s string
16+
}
17+
18+
// ans 是答案
19+
type ans301 struct {
20+
ans []string
21+
}
22+
23+
func Test_Problem301(t *testing.T) {
24+
25+
qs := []question301{
26+
27+
{
28+
para301{"()())()"},
29+
ans301{[]string{"(())()", "()()()"}},
30+
},
31+
32+
{
33+
para301{"(a)())()"},
34+
ans301{[]string{"(a())()", "(a)()()"}},
35+
},
36+
37+
{
38+
para301{")("},
39+
ans301{[]string{""}},
40+
},
41+
}
42+
43+
fmt.Printf("------------------------Leetcode Problem 301------------------------\n")
44+
45+
for _, q := range qs {
46+
_, p := q.ans301, q.para301
47+
fmt.Printf("【input】:%v 【output】:%v\n", p, removeInvalidParentheses(p.s))
48+
}
49+
fmt.Printf("\n\n\n")
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [301. Remove Invalid Parentheses](https://leetcode-cn.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

0 commit comments

Comments
 (0)