LeetCode : Generate Parentheses [java]
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
思路:使用遞迴方法,
規則是:
a. '('沒加完,就可以加'(',
b. ')'加入需要滿足兩點:1.')'沒加完;
2. 剩餘'('數小於剩餘')'數,加上')'才能保證可能匹配
import java.util.ArrayList; import java.util.List; public class Solution { public List<String> generateParenthesis(int n) { List<String> result = new ArrayList<>(); String s = ""; int left = n; int right = n; generate(left, right, s, result); return result; } private void generate(int left, int right, String s, List<String> result) { if (left == 0 && right == 0) { result.add(s); } if (left > 0) { generate(left - 1, right, s + "(", result); } if (right > 0 && left < right) { generate(left, right - 1, s + ")", result); } } }
相關推薦
LeetCode : Generate Parentheses [java]
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthe
【LeetCode】22. Generate Parentheses - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given n pairs of parentheses, write a function to generate all combinati
leetcode-22-括號生成(generate parentheses)-java
題目及測試 package pid022; /* 生成括號 給出 n 代表生成括號的對數,請你寫出一個函式,使其能夠生成所有可能的並且有效的括號組合。 例如,給出 n = 3,生成結果為: [ "((()))", "(()())", "(())()", "()(())"
LeetCode—generate-parentheses(所有括號的組合)—java
題目描述:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a so
[leetcode] Generate Parentheses
turn 時間 adding pre dot leetcode ive question 什麽 Given n pairs of parentheses, write a function to generate all combinations of well-for
[LeetCode] Generate Parentheses 生成括號
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "(
LeetCode Generate Parentheses 深度分析理解
Generate Parentheses Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, g
LeetCode: Generate Parentheses
Problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n
[leetcode]Generate Parentheses 生成圓括號 python實現
Generate Parentheses生成圓括號 Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthese
LeetCode——Generate Parentheses
href 分享 n) borde parent static cal htm family Given?n?pairs of parenthes
(Java)LeetCode-22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a so
LeetCode 22 Generate Parentheses (C,C++,Java,Python)
Problem: Given n pairs of parentheses, write a function to generate all combinations of well-forme
【LeetCode】022. Generate Parentheses
ret logs int false return 題解 gen cto solution 題目: Given n pairs of parentheses, write a function to generate all combinations of well-for
[LeetCode] 22. Generate Parentheses 生成括號
long array and code air sisd www str ons Given n pairs of parentheses, write a function to generate all combinations of well-formed paren
leetcode 22-Generate Parentheses(medium)
The new add col turn leet edi left array backtracking class Solution { public List<String> generateParenthesis(int n) {
leetcode#22. Generate Parentheses
rate gin != lee nth 可能 tco leetcode generate 給出 n 代表生成括號的對數,請你寫出一個函數,使其能夠生成所有可能的並且有效的括號組合。 例如,給出 n = 3,生成結果為: [ "((()))", "(()())", "(())
LeetCode 22. Generate Parentheses
需要 ret 16px pub valid bin amp 流程 des Given n pairs of parentheses, write a function to generate all combinations of well-formed paren
leetcode-java-20-有效的括號(valid parentheses)-java
題目及測試 package pid020; /* 有效的括號 給定一個只包括 '(',')','{','}','[',']' 的字串,判斷字串是否有效。 有效字串需滿足: 左括號必須用相同型別的右括號閉合。 左括號必須以正確的順序閉合。 注意空字串可被認為是有
【LeetCode】108.Generate Parentheses
題目描述(Medium) Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given&
LeetCode Day20 Generate Parentheses
法一:遞迴DFS 列出所有結果,考慮遞迴。由於字串只有左括號和右括號兩種字元,而且最終結果必定是左括號n個,右括號n個,所以我們定義兩個變數left和right分別表示剩餘左右括號的個數,如果在某次遞迴時,左括號的個數大於右括號的個數,說明此時生成的字串中右括號的個數大於左括號的個數,直接返