leetcode #22 in cpp
2016-05-22 04:49
423 查看
The question is to generate all possible combinations of valid parentheses given the number of pair of parentheses.
Solution:
We see it is going to get all possible combinations, which reminds us that recurrence is a possible solution.
To solve this, we have to think about what accounts for a valid generation of the valid parentheses.
Requirement 1: the number of left parentheses should never less than the one of right parentheses.
Requirement 2: the number of left parentheses should never more than n, which is given.
Any violation of these two requirement would generate invalid parentheses.
This leads to a method of generating a valid parenthesis:
Given a string of valid parenthesis:
if its number of left parentheses is less than n:
if its number of left parentheses is more than the number of right parentheses, we could append 1. one left parentheses to this string; 2. one right
parentheses to this string;
if its number of left parentheses is equal to the number of right parentheses , we could append one left parentheses to this string;
else if its number of left parentheses is equal to n:
We stop appending left parentheses. We append right parentheses until the number of right parentheses reaches n. And then add this string to the final
collection.
Each time after we append a parenthesis, we then pass the new string to the method again.
Code:
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
if(n == 0) return res;
if(n==1) return vector<string>{"()"};
string s = "";
generate(0,0,n,s, &res);
return res;
}
void generate(int left, int right, int n,string s, vector<string> *res){
if(left == n){
while(right < n){
s+=')';
right ++;
}
res->push_back(s);
}else{
if(left > right){
generate(left, right + 1, n, s+')',res);
}
generate(left + 1, right, n, s+'(',res);
}
}
};
Solution:
We see it is going to get all possible combinations, which reminds us that recurrence is a possible solution.
To solve this, we have to think about what accounts for a valid generation of the valid parentheses.
Requirement 1: the number of left parentheses should never less than the one of right parentheses.
Requirement 2: the number of left parentheses should never more than n, which is given.
Any violation of these two requirement would generate invalid parentheses.
This leads to a method of generating a valid parenthesis:
Given a string of valid parenthesis:
if its number of left parentheses is less than n:
if its number of left parentheses is more than the number of right parentheses, we could append 1. one left parentheses to this string; 2. one right
parentheses to this string;
if its number of left parentheses is equal to the number of right parentheses , we could append one left parentheses to this string;
else if its number of left parentheses is equal to n:
We stop appending left parentheses. We append right parentheses until the number of right parentheses reaches n. And then add this string to the final
collection.
Each time after we append a parenthesis, we then pass the new string to the method again.
Code:
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
if(n == 0) return res;
if(n==1) return vector<string>{"()"};
string s = "";
generate(0,0,n,s, &res);
return res;
}
void generate(int left, int right, int n,string s, vector<string> *res){
if(left == n){
while(right < n){
s+=')';
right ++;
}
res->push_back(s);
}else{
if(left > right){
generate(left, right + 1, n, s+')',res);
}
generate(left + 1, right, n, s+'(',res);
}
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解