一天一道LeetCode

(一)题目

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一文中提到怎么判断该组括号有效,另外在【一天一道LeetCode】#17. Letter Combinations of a Phone Number一文中用到回溯法。本题结合这两题的思路来解决。

假设在位置k之后,如果’(‘的数量还有剩余则可以继续添加;那么要添加’)’,则必须满足其剩余的数量大于0且比’(‘剩余的数量大,才可以添加,否则就不是一个有效的括号组。

class Solution {
public:
    vector<string> ret;
    vector<string> generateParenthesis(int n) {
        string str;
        generate(str , n , n);
        return ret;
    }
    void generate(string str , int m , int n)
    {
        if(m==0)//
        {
            while(n--) str+=')';//如果‘(’没有剩余的了,就直接把剩余的')'加上
            ret.push_back(str);
            return;
        }
        if(m>0)//如果‘(’剩余,可以继续添加'('
        {
            generate(str+'(',m-1,n);
        }
        if(m<n&&n>0)//如果m<n且n>0,可以继续添加')'
        {
            generate(str+')',m,n-1);
        }
    }
};

【一天一道LeetCode】#22. Generate Parentheses的更多相关文章

  1. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  3. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  5. [leetcode]22. Generate Parentheses生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. [LeetCode] 22. Generate Parentheses ☆☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. [LeetCode]22. Generate Parentheses括号生成

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  10. [leetcode] 22. Generate Parentheses(medium)

    原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...

随机推荐

  1. django+uwsgi+nginx+postgresql备忘

    安装pg创建数据库xxx设置用户密码111111 apt-get install postgresql su - postgres psql create database xxx; alter us ...

  2. SpringBatch简介

    spring Batch是一个轻量级的.完善的批处理框架,旨在帮助企业建立健壮.高效的批处理应用.SpringBatch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使 ...

  3. 没有JavaScript的基础,我可以学习Angular2吗?

    Can I learn and understand Angular2 without understanding JavaScript? 没有JavaScript基础我能学习和理解Angular2吗 ...

  4. python语言学习

    前段时间要做视频直播需要编写自动模块,就考虑使用python脚本语言,python的好多语法都是很独特的,比如数据类型不需要预定义,缩进的方式等,另外功能也很强大,豆瓣就是用python写的.我写的部 ...

  5. Android必知必会-Android Studio下配置和使用Lambda

    移动端如果访问不佳,请访问–>Github版 背景 和朋友讨论 JAVA8 的新特性,聊到Lambda,正好在掘金上看到一篇相关的文章,结合资料,作一个总结,特别是记录下实际使用中遇到的问题. ...

  6. x264源代码简单分析:x264_slice_write()

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  7. MySQL 存储过程探秘

    关于存储过程的优点,本文不再阐述.这里只是对创建存储过程时可能遇到的问题做一下简单的分析. 必备基础 这里说的基础,是相关于如何创建一个存储过程的. DELIMITER:分隔符,定界符. 作用就是:作 ...

  8. Linux2.6--虚拟文件系统

          虚拟文件系统(有时也称作虚拟文件交换,更常见的是简称做VFS)作为内核子系统,为用户空间程序提供了文件和文件系统相关的接口.系统中的所有文件系统不但依赖VFS共存,而且也依赖VFS系统协同 ...

  9. Android View架构总结

    View和Activity的区别 android的四大组件,Activity是四大组件中唯一一个用来和用户进行交互的组件.可以说Activity就是android的视图层. 如果再细化,Activit ...

  10. 使用maven将项目打成jar包

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...