参考 generate-parentheses

版权声明:本文为博主原创文章,未经博主允许不得转载。
题目:http://oj.leetcode.com/problems/generate-parentheses/
描述:给定一个非负整数n,生成n对括号的所有合法排列。
解答:
该问题解的个数就是卡特兰数,但是现在不是求个数,而是要将所有合法的括号排列打印出来。
该问题和《编程之美》的买票找零问题一样,通过买票找零问题我们可以知道,针对一个长度为2n的合法排列,第1到2n个位置都满足如下规则:左括号的个数大于等于右括号的个数。所以,我们就可以按照这个规则去打印括号:假设在位置k我们还剩余left个左括号和right个右括号,如果left>0,则我们可以直接打印左括号,而不违背规则。能否打印右括号,我们还必须验证left和right的值是否满足规则,如果left>=right,则我们不能打印右括号,因为打印会违背合法排列的规则,否则可以打印右括号。如果left和right均为零,则说明我们已经完成一个合法排列,可以将其打印出来。通过深搜,我们可以很快地解决问题,针对n=2,问题的解空间如下:
按照这种思路,代码如下:
- void generate(int leftNum,int rightNum,string s,vector<string> &result)
- {
- if(leftNum==0&&rightNum==0)
- {
- result.push_back(s);
- }
- if(leftNum>0)
- {
- generate(leftNum-1,rightNum,s+'(',result);
- }
- if(rightNum>0&&leftNum<rightNum)
- {
- generate(leftNum,rightNum-1,s+')',result);
- }
- }
网上对该问题的解答非常多,无一例外都采用了递归,但是鲜见和上面思路如此清晰的算法。上述算法的思路是很多问题的通解,值得仔细研究。
作为一个例子,看一下数组的入栈出栈顺序问题:给定一个长度为n的不重复数组,求所有可能的入栈出栈顺序。该问题解的个数也是卡特兰数,根据上面的思路,我们也可以写出一个类似的代码:
- void inoutstack(int in,int out,deque<int> &q,stack<int> &s,deque<int> seq,vector<deque<int>> &result)
- {
- if(!in&&!out)
- {
- result.push_back(q);
- return;
- }
- if(in>0)
- {
- s.push(seq.front());
- seq.pop_front();
- inoutstack(in-1,out,q,s,seq,result);
- seq.push_front(s.top());
- s.pop();
- }
- if(out>0&&in<out)
- {
- q.push_back(s.top());
- s.pop();
- inoutstack(in,out-1,q,s,seq,result);
- s.push(q.back());
- q.pop_back();
- }
- }
上述代码由于采用了栈和队列模仿整个过程,所以显得略微复杂,但是代码的基本结构还是符合一个类似的基本规则:在某一个特定时刻,入栈的次数大于或者等于出栈的次数。在生成括号的问题中,我们利用一个string来保存结果,由于打印左括号时不影响打印右括号,所以无需复杂的状态恢复。在入栈出栈顺序问题中,由于两次递归调用共享同一个栈和队列,所以我们需要手动恢复其内容。在恢复时,队列会从头部删除和添加,所以我们采用了deque,它可以在头部添加和删除元素。queue只能在头部删除元素,所以没有采用。
参考 generate-parentheses的更多相关文章
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode022. Generate Parentheses
leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- leetcode-algorithms-22 Generate Parentheses
leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...
随机推荐
- 在页面左右一个悬浮div兼容IE6 IE7 8 9 Firefox chrome
在页面左右一个悬浮div兼容IE6 IE7 8 9 Firefox chrome #identifier-pannel { bottom: 345px; margin-left: 512px; pos ...
- 什么是Cookie。Cookie的原理介绍,Cookie的简单应用
1 介绍:Cookies亦称Cookie .Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术.Cookies是当你浏览某网站时,由Web服务 ...
- 十分钟使用ionic Framework开发一个跨平台移动应用
Ionic是一个前端的框架,帮助开发人员使用HTML5, CSS3和JavaScript做出原生应用. ionic的理念类似前端开发的BootStrap,目标是封装HTML5移动跨平台开发的最佳实践. ...
- 如何查看LoadRunner虚拟用户(vuser)类型
查看vuser类型LoadRunner提供了多种Vuser技术,通过这些技术可以在使用不同类型的客户端/服务器体系结构时生成服务器负载.每种Vuser技术都适合于特定体系结构并产生特定的Vuser类型 ...
- 解决64位操作系统下运行psql的问题
我的机器环境是 win7 64位.正常情况下,只需要安装plsql 和 oracle client,配置好 tnsname.ora 就能正常访问数据库.实际上遇到了几个问题. 1. 在tnsnames ...
- Android源码-SignApk.java
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- 《将一个字符串转换成datetime时,先分析该字符串以获取日期,然后再将每个变量放置到datetime对象中》的解决办法
我们在写代码时,稍不注意就收到VS那文不对题的错误提示. 最近在项目上碰到了“将一个字符串转换成datetime时,先分析该字符串以获取日期,然后再将每个变量放置到datetime对象中”的这个错误提 ...
- jms、amqp、mqtt区别与联系
消息传递作为基本通信机制已经在全世界成功运用.无论是人与人.机器与人还是机器与机器之间,消息传递一直都是唯一常用的通信方式.在双方(或更多)之间交换消息有两种基本机制. 同步消息传递 异步消息传递 同 ...
- android 中的 Handler 线程间通信
一. 在MainActivity中为什么只是类似的写一行如下代码就可以使用handler了呢? Handler handler = new Handler() { @Override public v ...
- CI框架源代码阅读笔记7 配置管理组件 Config.php
原文见这里:http://www.cnblogs.com/ohmygirl/p/CIRead-7.html 一个灵活可控的应用程序中,必定会存在大量的可控參数(我们称为配置),比如在CI的主配置文件里 ...