日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

【LeetCode笔记】301. 删除无效的括号(Java、DFS、字符串)

發(fā)布時(shí)間:2024/7/23 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode笔记】301. 删除无效的括号(Java、DFS、字符串) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 題目描述
  • 思路 && 代碼
      • 二刷

題目描述

  • 【所有可能結(jié)果】-> 【暴力DFS】

思路 && 代碼

  • 代碼比較長(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; // 維護(hù)有效字符串的最長(zhǎng)值public List<String> removeInvalidParentheses(String s) {char[] arr = s.toCharArray();int right = 0;// 右括號(hào)計(jì)數(shù):用于 dfs 剪枝(選取左括號(hào)數(shù)量,一定不能大于右括號(hào)數(shù)量)for(char c : arr) {if(c == ')') {right++;}}// Set 用于去重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) {// Case 1: 結(jié)束if(index == cs.length) {// 合法,并且長(zhǎng)度夠的情況if(score == 0 && cur.length() >= len) {// 加入 Set 中,并且維護(hù) lenlen = cur.length();ans.add(cur.toString());}return;}// Case 2:當(dāng)前為左括號(hào)if(cs[index] == '(') {// Case 2.1:選擇【加入】當(dāng)前左括號(hào)(如果 score + 1 > max,說(shuō)明后面右括號(hào)【肯定】不夠了)if(score + 1 <= max) {dfs(cs, index + 1, score + 1, max, cur.append('('), ans);cur.deleteCharAt(cur.length() - 1);}// Case 2.2:選擇【不加入】當(dāng)前左括號(hào)dfs(cs, index + 1, score, max, cur, ans);}// Case 3:當(dāng)前為右括號(hào)else if(cs[index] == ')') {// Case 3.1:選擇【加入】當(dāng)前右括號(hào)(左邊有提供左括號(hào))if(score > 0) {dfs(cs, index + 1, score - 1, max, cur.append(')'), ans);cur.deleteCharAt(cur.length() - 1);}// Case 3.2:選擇【不加入】當(dāng)前右括號(hào)dfs(cs, index + 1, score, max, cur, ans);}// Case 4:當(dāng)前為字母,直接【加入】,不影響else {dfs(cs, index + 1, score, max, cur.append(cs[index]), ans);cur.deleteCharAt(cur.length() - 1);}} }
  • 無(wú)注釋版
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ò),歡迎將生活随笔推薦給好友。