C++實(shí)現(xiàn)LeetCode(22.生成括號(hào))
[LeetCode] 22. Generate Parentheses 生成括號(hào)
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
這道題給定一個(gè)數(shù)字n,讓生成共有n個(gè)括號(hào)的所有正確的形式,對(duì)于這種列出所有結(jié)果的題首先還是考慮用遞歸 Recursion 來解,由于字符串只有左括號(hào)和右括號(hào)兩種字符,而且最終結(jié)果必定是左括號(hào)3個(gè),右括號(hào)3個(gè),所以這里定義兩個(gè)變量 left 和 right 分別表示剩余左右括號(hào)的個(gè)數(shù),如果在某次遞歸時(shí),左括號(hào)的個(gè)數(shù)大于右括號(hào)的個(gè)數(shù),說明此時(shí)生成的字符串中右括號(hào)的個(gè)數(shù)大于左括號(hào)的個(gè)數(shù),即會(huì)出現(xiàn) ')(' 這樣的非法串,所以這種情況直接返回,不繼續(xù)處理。如果 left 和 right 都為0,則說明此時(shí)生成的字符串已有3個(gè)左括號(hào)和3個(gè)右括號(hào),且字符串合法,則存入結(jié)果中后返回。如果以上兩種情況都不滿足,若此時(shí) left 大于0,則調(diào)用遞歸函數(shù),注意參數(shù)的更新,若 right 大于0,則調(diào)用遞歸函數(shù),同樣要更新參數(shù),參見代碼如下:
C++ 解法一:
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
generateParenthesisDFS(n, n, "", res);
return res;
}
void generateParenthesisDFS(int left, int right, string out, vector<string> &res) {
if (left > right) return;
if (left == 0 && right == 0) res.push_back(out);
else {
if (left > 0) generateParenthesisDFS(left - 1, right, out + '(', res);
if (right > 0) generateParenthesisDFS(left, right - 1, out + ')', res);
}
}
};
Java 解法一:
public class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<String>();
helper(n, n, "", res);
return res;
}
void helper(int left, int right, String out, List<String> res) {
if (left < 0 || right < 0 || left > right) return;
if (left == 0 && right == 0) {
res.add(out);
return;
}
helper(left - 1, right, out + "(", res);
helper(left, right - 1, out + ")", res);
}
}
再來看那一種方法,這種方法是 CareerCup 書上給的方法,感覺也是滿巧妙的一種方法,這種方法的思想是找左括號(hào),每找到一個(gè)左括號(hào),就在其后面加一個(gè)完整的括號(hào),最后再在開頭加一個(gè) (),就形成了所有的情況,需要注意的是,有時(shí)候會(huì)出現(xiàn)重復(fù)的情況,所以用set數(shù)據(jù)結(jié)構(gòu),好處是如果遇到重復(fù)項(xiàng),不會(huì)加入到結(jié)果中,最后我們?cè)侔裺et轉(zhuǎn)為vector即可,參見代碼如下::
n=1: ()
n=2: (()) ()()
n=3: (()()) ((())) ()(()) (())() ()()()
C++ 解法二:
class Solution {
public:
vector<string> generateParenthesis(int n) {
unordered_set<string> st;
if (n == 0) st.insert("");
else {
vector<string> pre = generateParenthesis(n - 1);
for (auto a : pre) {
for (int i = 0; i < a.size(); ++i) {
if (a[i] == '(') {
a.insert(a.begin() + i + 1, '(');
a.insert(a.begin() + i + 2, ')');
st.insert(a);
a.erase(a.begin() + i + 1, a.begin() + i + 3);
}
}
st.insert("()" + a);
}
}
return vector<string>(st.begin(), st.end());
}
};
Java 解法二:
public class Solution {
public List<String> generateParenthesis(int n) {
Set<String> res = new HashSet<String>();
if (n == 0) {
res.add("");
} else {
List<String> pre = generateParenthesis(n - 1);
for (String str : pre) {
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == '(') {
str = str.substring(0, i + 1) + "()" + str.substring(i + 1, str.length());
res.add(str);
str = str.substring(0, i + 1) + str.substring(i + 3, str.length());
}
}
res.add("()" + str);
}
}
return new ArrayList(res);
}
}
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(22.生成括號(hào))的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)生成括號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(28.實(shí)現(xiàn)strStr()函數(shù))
- C++實(shí)現(xiàn)LeetCode(27.移除元素)
- C++實(shí)現(xiàn)LeetCode(83.移除有序鏈表中的重復(fù)項(xiàng))
- C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))
- C++實(shí)現(xiàn)LeetCode(25.每k個(gè)一組翻轉(zhuǎn)鏈表)
- C++實(shí)現(xiàn)LeetCode(88.混合插入有序數(shù)組)
- C++實(shí)現(xiàn)LeetCode(21.混合插入有序鏈表)
- C++實(shí)現(xiàn)LeetCode(31.下一個(gè)排列)
相關(guān)文章
基于C++實(shí)現(xiàn)柏林噪聲算法(Perlin?Noise)
Perlin噪聲(Perlin?noise,又稱為柏林噪聲)指由Ken?Perlin發(fā)明的自然噪聲生成算法,具有在函數(shù)上的連續(xù)性,并可在多次調(diào)用時(shí)給出一致的數(shù)值。本文將用C++實(shí)現(xiàn)柏林噪聲算法,感興趣的可以了解一下2023-03-03
FFmpeg實(shí)戰(zhàn)之分離出PCM數(shù)據(jù)
PCM(Pulse?Code?Modulation,脈沖編碼調(diào)制)音頻數(shù)據(jù)是未經(jīng)壓縮的音頻采樣數(shù)據(jù)裸流,它是由模擬信號(hào)經(jīng)過采樣、量化、編碼轉(zhuǎn)換成的標(biāo)準(zhǔn)數(shù)字音頻數(shù)據(jù)。本文將通過FFmpeg實(shí)現(xiàn)分離PCM數(shù)據(jù),感興趣的可以了解一下2023-02-02
C語言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼
今天小編就為大家分享一篇C語言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
C語言回溯法 實(shí)現(xiàn)組合數(shù) 從N個(gè)數(shù)中選擇M個(gè)數(shù)
在平時(shí)的算法的題目中,時(shí)常會(huì)遇到組合數(shù)相關(guān)的問題,暴力枚舉。在N個(gè)數(shù)中挑選M個(gè)數(shù)出來。利用for循環(huán)也可以處理,但是可拓展性不強(qiáng),于是寫這個(gè)模板供以后參考2018-08-08

