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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

递归/回溯:Generate Parentheses生成合法括号

發(fā)布時(shí)間:2023/11/27 生活经验 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 递归/回溯:Generate Parentheses生成合法括号 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

已知n組括號(hào),開發(fā)一個(gè)程序,生成這n組括號(hào)所有的合法的組合可能。 例如:n = 3
結(jié)果為: ["((()))", “(()())”, “(())()”, “()(())”, “()()()”]

首先思考如何生成所有的括號(hào)組合的可能性,即例如2組括號(hào),總共4個(gè)符號(hào)組合的可能型,那么每個(gè)位置就有兩種括號(hào)的可能性,要么左括號(hào),要么右括號(hào),此時(shí)可以寫出如代碼:

void generate(string item, int n, std::vector<string> &result) {if (item.size() == 2 * n) {result.push_back(item);return ;}generate(item + "(", n , result);generate(item + ")", n , result);
} std::vector<string> generate_parenthesed(int n) {std::vector<string> result;generate("", n, result);return result;
}

測(cè)試如上代碼,可以看到2組括號(hào)總共可能有16中組合的可能性:

2
((((
((()
(()(
(())
()((
()()
())(
()))
)(((
)(()
)()(
)())
))((
))()
)))(
))))

但是根據(jù)輸出結(jié)果,我們顯然能夠看出來很多結(jié)果并不符合 合法括號(hào)的要求,那么什么是合法的括號(hào)呢?或者說如何生成一個(gè)合法的括號(hào)呢?

根據(jù)括號(hào)的規(guī)律:

  1. 初始括號(hào)一定是左括號(hào)
  2. 括號(hào)集中左括號(hào)的數(shù)目一定和右括號(hào)的數(shù)目相等
  3. 添加括號(hào)的過程中如果左括號(hào)的數(shù)目大于右括號(hào),則才能夠添加右括號(hào),否則不能添加右括號(hào)

基于以上過程,實(shí)現(xiàn)如下:

/*left和right代表剩余括號(hào)數(shù),left代表還剩下多少個(gè)左括號(hào)未添加,right代表還剩下多少個(gè)右括號(hào)未添加*/
void generate_leagal(string item, int left, int right, std::vector<string> &result) {if(left == 0 && right == 0) {result.push_back(item);return;}if(left > 0) { //當(dāng)還有剩余的左括號(hào)未添加,則添加左括號(hào)generate_leagal(item + "(", left - 1, right, result);}if (left < right) { //當(dāng)已添加的左括號(hào)的數(shù)目大于右括號(hào),則才能夠添加右括號(hào)generate_leagal(item + ")", left, right - 1, result);}
}std::vector<string> generate_parenthesed(int n) {std::vector<string> result;generate_leagal("", n, n, result);return result;
}

測(cè)試代碼如下:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>/*
已知n組括號(hào),開發(fā)一個(gè)程序,生成這n組括號(hào)所有的合法的組合可能。 例如:n = 3
結(jié)果為: ["((()))", "(()())", "(())()", "()(())", "()()()"]
*/using namespace std;void generate_leagal(string item, int left, int right, std::vector<string> &result) {if(left == 0 && right == 0) {result.push_back(item);return;}if(left > 0) {generate_leagal(item + "(", left - 1, right, result);}if (left < right) {generate_leagal(item + ")", left, right - 1, result);}
}std::vector<string> generate_parenthesed(int n) {std::vector<string> result;generate_leagal("", n, n, result);return result;
}int main(int argc, char const *argv[])
{int n;std::vector<string> result;cin >> n;result = generate_parenthesed(n);for (int i = 0;i < result.size(); ++i) {cout << result[i] << endl;}return 0;
}

輸出如下:

#輸入
4
#輸出
(((())))
((()()))
((())())
((()))()
(()(()))
(()()())
(()())()
(())(())
(())()()
()((()))
()(()())
()(())()
()()(())
()()()()

總結(jié)

以上是生活随笔為你收集整理的递归/回溯:Generate Parentheses生成合法括号的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。