生活随笔
收集整理的這篇文章主要介紹了
【LeetCode笔记】301. 删除无效的括号(Java、DFS、字符串)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
題目描述
思路 && 代碼
- 代碼比較長(zhǎng),但是總體思路很清晰。
- 剪枝:舍棄左括號(hào)、舍棄右括號(hào)兩種情況(見(jiàn)注釋)
- 分情況:當(dāng)前字符有【左括號(hào)】、【右括號(hào)】、【字母】三種情況,字母直接取,不影響
- 具體見(jiàn)注釋的 Case 分類(lèi),有清晰說(shuō)明
- 去重:先通過(guò) Set 存儲(chǔ)所有當(dāng)前可行解
- 篩選:dfs結(jié)束后,len 為刪除最小數(shù)量無(wú)效括號(hào)后的字符串長(zhǎng)度。用于對(duì) Set 中的有效括號(hào)進(jìn)行篩選。
class Solution {int len
; public List<String> removeInvalidParentheses(String s
) {char[] arr
= s
.toCharArray();int right
= 0;for(char c
: arr
) {if(c
== ')') {right
++;}}Set<String> set
= new HashSet<>();dfs(arr
, 0, 0, right
, new StringBuilder(), set
);List<String> ans
= new ArrayList<>();for(String str
: set
) {if(str
.length() == len
) {ans
.add(str
);}}return ans
;}public void dfs(char[] cs
, int index
, int score
, int max
, StringBuilder cur
, Set<String> ans
) {if(index
== cs
.length
) {if(score
== 0 && cur
.length() >= len
) {len
= cur
.length();ans
.add(cur
.toString());}return;}if(cs
[index
] == '(') {if(score
+ 1 <= max
) {dfs(cs
, index
+ 1, score
+ 1, max
, cur
.append('('), ans
);cur
.deleteCharAt(cur
.length() - 1);}dfs(cs
, index
+ 1, score
, max
, cur
, ans
);}else if(cs
[index
] == ')') {if(score
> 0) {dfs(cs
, index
+ 1, score
- 1, max
, cur
.append(')'), ans
);cur
.deleteCharAt(cur
.length() - 1);}dfs(cs
, index
+ 1, score
, max
, cur
, ans
);}else {dfs(cs
, index
+ 1, score
, max
, cur
.append(cs
[index
]), ans
);cur
.deleteCharAt(cur
.length() - 1);}}
}
class Solution {int len
; public List<String> removeInvalidParentheses(String s
) {char[] arr
= s
.toCharArray();int right
= 0;for(char c
: arr
) {if(c
== ')') {right
++;}}Set<String> set
= new HashSet<>();dfs(arr
, 0, 0, right
, new StringBuilder(), set
);List<String> ans
= new ArrayList<>();for(String str
: set
) {if(str
.length() == len
) {ans
.add(str
);}}return ans
;}public void dfs(char[] cs
, int index
, int score
, int max
, StringBuilder cur
, Set<String> ans
) {if(index
== cs
.length
) {if(score
== 0 && cur
.length() >= len
) {len
= cur
.length();ans
.add(cur
.toString());}return;}if(cs
[index
] == '(') {if(score
+ 1 <= max
) {dfs(cs
, index
+ 1, score
+ 1, max
, cur
.append('('), ans
);cur
.deleteCharAt(cur
.length() - 1);}dfs(cs
, index
+ 1, score
, max
, cur
, ans
);}else if(cs
[index
] == ')') {if(score
> 0) {dfs(cs
, index
+ 1, score
- 1, max
, cur
.append(')'), ans
);cur
.deleteCharAt(cur
.length() - 1);}dfs(cs
, index
+ 1, score
, max
, cur
, ans
);}else {dfs(cs
, index
+ 1, score
, max
, cur
.append(cs
[index
]), ans
);cur
.deleteCharAt(cur
.length() - 1);}}
}
二刷
- 二刷懵逼的地方:合法性維護(hù)
- 靠 score 來(lái)進(jìn)行,眾所周知 ‘(’ 隨便加,等跑到結(jié)尾再算帳 (score == 0)
- 但 ‘)’ 不一樣,得前面的 ‘(’ 數(shù)量夠才行。( if(score > 0) )
- 由此上兩點(diǎn),可得出合法判斷
- 【選出合法結(jié)果,維護(hù)最長(zhǎng)合法長(zhǎng)度】-》【由最終長(zhǎng)度,篩選較小結(jié)果】
- 來(lái)了,無(wú)剪枝,無(wú)效率優(yōu)化的寫(xiě)法!咱圖的就是一個(gè)簡(jiǎn)單明了!擺爛,慢得一
- 具體是少了 StringBuilder、char[] 等優(yōu)化,但有效代碼就 22 行,很簡(jiǎn)約!
class Solution {int maxLen
= 0;Set<String> set
= new HashSet<>();public List<String> removeInvalidParentheses(String s
) {dfs(0, 0, "", s
);List<String> ans
= new ArrayList<>();for(String temp
: set
) {if(temp
.length() == maxLen
) {ans
.add(temp
);}}return ans
;}void dfs(int score
, int index
, String now
, String s
) {if(index
== s
.length()) {if(score
== 0 && now
.length() >= maxLen
) {maxLen
= now
.length();set
.add(now
);} }else if(s
.charAt(index
) == '(') {dfs(score
+ 1, index
+ 1, now
+ '(', s
);dfs(score
, index
+ 1, now
, s
);}else if(s
.charAt(index
) == ')') {if(score
> 0) {dfs(score
- 1, index
+ 1, now
+ ')', s
);}dfs(score
, index
+ 1, now
, s
);}else {dfs(score
, index
+ 1, now
+ s
.charAt(index
), s
);}}
}
總結(jié)
以上是生活随笔為你收集整理的【LeetCode笔记】301. 删除无效的括号(Java、DFS、字符串)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。