leetCode 22.Generate Parentheses (生成括號) 解題思路和方法
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
思路:此題也是不算太難的問題,不過剛開始沒有思路,慢慢想,最終經過思考,形成遞迴演算法,即從一個括號()開始,到兩個括號()()、(()),到三個括號()()(),()(()),(())(),((())),其實是一個把()插入到前面字串的過程,只不過本題不能重複,所以還需要hashSet去除重複。
具體程式碼如下:
public class Solution { Set<String> set = new HashSet<String>();//用於消除重複值 public List<String> generateParenthesis(int n) { List<String> list = new ArrayList<String>(); if(n == 0){ return list; } list.add("()");//因為n>=1,故list初始化1對 //迴圈,每次增加一對(),並返回,直到最後完成 for(int i = 1; i < n; i++){ list = addList(list); } return list; } //每次增加一對()並返回無重複的新的list List<String> addList(List<String> list){ List<String> al = new ArrayList<String>(); int size = list.size();//list已有資料個數 String temp;//暫時字串 for(int i = 0; i < size;i++){ String s = list.get(i);//取出 temp = ""; //對取出的字串迴圈插入(),不重複即插入list for(int j = 0;j < s.length();j++){ temp = s.substring(0,j) + "()" + s.substring(j); if(set.add(temp)){//判斷是否已有 al.add(temp); } } } return al; } }
相關推薦
leetCode 22.Generate Parentheses (生成括號) 解題思路和方法
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For exampl
[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 生成括號 Python 回溯解法
題目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 給定n對括號,寫一個函式來生成成對的括號
22. Generate Parentheses 生成括號
ted mar lose solution write (()) http table for Given n pairs of parentheses, write a function to generate all combinations of well-form
leetCode 27.Remove Element (刪除元素) 解題思路和方法
value ava leetcode ont bsp lac 方法 -s post Remove Element Given an array and a value, remove all instances of that value in plac
leetCode 57.Insert Interval (插入區間) 解題思路和方法
use example ava article urn win time 例如 ont Insert Interval Given a set of non-overlapping intervals, insert a new interval into
leetCode 72.Edit Distance (編輯距離) 解題思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to conve
leetCode 49.Anagrams (迴文構詞法) 解題思路和方法
Anagrams Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case. 思路:這題要是解,必須知
leetCode 48.Rotate Image (旋轉影象) 解題思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees
leetCode 67.Add Binary (二進位制加法) 解題思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" R
leetcode-22.Generate Parentheses 括號生成
題目: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, give
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
[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 solution set
LeetCode 22 Generate Parentheses(生成括號)
classSolution{public:/** * @param n n pairs * @return All combinations of well-formed parentheses */ vector<string> generate
[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 22. Generate Parentheses(生成合法圓括號序列)
題目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed p
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]22. Generate Parentheses
Solution 1: 遞歸回溯法 回溯就是找到所有的解,一直沒用java寫過回溯,突然有點懵逼 一直往左下走 if (open < max) backtrack(ans, cur+"(", open+1, close, max); cur還是