leetcode 22 Generate Parentheses关于C++ string的问题

问题描述

leetcode 22 Generate Parentheses关于C++ string的问题
 /*
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:

"((()))", "(()())", "(())()", "()(())", "()()()"
*/

/*
不是很懂p = 2的时候我的结果是["(())","(()()"]。。。
到底哪里出错了。。。
*/
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        //如果当前写的左括号小于n,则可以再写一个左括号
        //如果当前写的右括号小于n且小于已写的左括号个数,则可以再写一个右括号
        vector<string> v;
        if(n == 0) return v;
        writeP("",0,0,n,v);
        return v;
    }
    void writeP(string s,int left,int right,int n,vector<string>& v){
        if(left==n&&right==n){
            v.push_back(s);
            return;
        }
        if(left < n){
           s = s + "(";
           writeP(s,left+1,right,n,v);
        }
        if(right < n && right < left){
           s = s + ")";
           writeP(s,left,right+1,n,v);
        }
    }
};
/*
把更改s的部分放在调用函数里面就AC了。。
*/
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        //如果当前写的左括号小于n,则可以再写一个左括号
        //如果当前写的右括号小于n且小于已写的左括号个数,则可以再写一个右括号
        vector<string> v;
        writeP("",0,0,n,v);
        return v;
    }
    void writeP(string s,int left,int right,int n,vector<string>& v){
        if(left < n){
          // s = s + "(";
           writeP(s+"(",left+1,right,n,v);
        }
        if(right < n && right < left){
          // s = s + ")";
           writeP(s+")",left,right+1,n,v);
        }
        if(left==n&&right==n){
            v.push_back(s);
            return;
        }
    }
};

解决方案

[LeetCode22]Generate Parentheses
Leetcode#22||Generate Parentheses
LeetCode 22.Generate Parentheses

解决方案二:

http://blog.csdn.net/feliciafay/article/details/17414981

时间: 2024-12-09 05:41:01

leetcode 22 Generate Parentheses关于C++ string的问题的相关文章

[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 is: "((()))", "(()())", "(())()", "()(())", "()()()" [分

LeetCode 22 Generate Parentheses(生成括号)

翻译 给定一个括号序列,写一个函数用于生成正确形式的括号组合. 例如,给定n = 3,一个解决方案集是: "((()))", "(()())", "(())()", "()(())", "()()()" 原文 Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes

【LeetCode从零单排】No22.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: "((()))", "(()())", "(())()", "()(())", "()()()" 代码 F

[LeetCode] 20.Valid Parentheses

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

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 20 Valid Parentheses 括号匹配

Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

[LeetCode]151.Reverse Words in a String

题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

[LeetCode]--345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". Note: The vowels does not incl

[LeetCode] Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()" -> ["()()()",