leetcode刷题, 总结,记录,备忘22

原创 2015年07月07日 20:41:30

leetcdoe22

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

第一想法是递归,可是自己写的递归错了,无奈去看别人的写法了,,,,,虽然看懂了,但是我自己并不能说的清楚,理解递归的最好的方法还是自己带个小点的数字进去自己实地的演练几次就会很轻松的了解。

class Solution {
public:
    void g(vector<string>& vs, string & s, int left, int right, int n)
    {
        if (left == n && right == n)
        {
            vs.push_back(s);
        }
        
        if (left < n)
        {
            s.push_back('(');
            g(vs, s, left+1, right, n);
            s.pop_back();
        }
        
        if (right < left)
        {
            s.push_back(')');
            g(vs, s, left, right+1, n);
            s.pop_back();
        }
    }
    
    vector<string> generateParenthesis(int n) {
        string s;
        vector<string> vs;
        g(vs, s, 0, 0, n);
        
        return vs;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode刷题,总结,记录,备忘 201

leetcode201Bitwise AND of Numbers Range Given a range [m, n] where 0 For example, given the rang...

leetcode刷题,总结,记录,备忘 223

leetcode223 Find the total area covered by two rectilinear rectangles in a 2D plane. Each re...

leetcode刷题, 总结,记录, 备忘142

leetcode142 Given a linked list, return the node where the cycle begins. If there is no cycle, retu...

leetcode刷题,总结,记录,备忘 313

leetcode313Super Ugly Number Write a program to find the nth super ugly number. Super ugly n...

leetcode刷题,总结,记录,备忘300

leetcode300,Longest Increasing Subsequence Given an unsorted array of integers, find the length...

leetcode刷题,总结,记录,备忘 39

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher...

leetcode刷题,总结,记录,备忘 88

leetcode88,Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nu...

leetcode刷题,总结,记录,备忘 312

leetcode312Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a...

leetcode刷题,总结,记录,备忘 19

leetcode19Remove Nth Node From End of List Given a linked list, remove the nth node from the en...

leetcode刷题,总结,记录,备忘 8

leetcode8 String to Integer (atoi)   Implement atoi to convert a string to an integer. Hint: Ca...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode刷题, 总结,记录,备忘22
举报原因:
原因补充:

(最多只允许输入30个字)