leetcode题解 Generate Parentheses
原文链接:https://leetcode.com/problems/generate-parentheses
给出数字n,求n对括号组成的合法字符串。
刚做出来,敲完代码,修改了一次,然后提交,accepted ~ 还是小有成就感的。。菜鸟...
先记录下自己的笨方法。再研究更高妙的方法。
刚看到题也是一头雾水。没法深入思考,感觉就是不断的罗列,被题目吓到了。
仔细观察,合法字符串的第一个字母肯定是( ,最后一个肯定是)。
对于合法字符串的每一位,必定有 左括号的个数 >= 右括号的个数。每一位都必须满足。
想法就是,从第一位开始向最后一位扩充,直到填满2*n位。
当填第i位的时候,考察前面左括号的个数left,和右括号的个数right。有下面三种情况:
1、left == right 左右括号相等了,这时候只能填左括号。
2、left == n , right < n 左括号已经有n个了,满了,只能填右括号了。
3、left < n && left > right 左括号还没满,且比右括号多。这时候,可以填两种。
本来想递推算,但是后来习惯性改成了循环。
用C太麻烦了!
C++的STL,边搜语法边照着敲的。肯定用的很蹩脚。见谅。
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<int>left; //第i个字符串左括号的个数
vector<int>right; //第i个字符串右括号个数
vector<string> ans;
int i,j,k;
string tmp("("); ans.push_back(tmp); //第一个字符串就是"("
left.push_back(); //第一个字符串的左括号有一个
right.push_back(); //右括号0。 for(i = ; i<*n; i++) //开始添加第i位。
{
int len = ans.size();
for(j = ;j < len; j++) //遍历每个字符串
{
if(left[j] == right[j])
{
ans[j].append(,'(');
left[j] ++;
}
else if(left[j]>right[j] && left[j] < n)
{
string tmp(ans[j]);
tmp.append(,'(');
ans.push_back(tmp);
left.push_back(left[j]+);
right.push_back(right[j]); ans[j].append(,')');
right[j]++;
}
else if(left[j] == n)
{
ans[j].append(,')');
right[j]++;
}
}
}
return ans;
}
};
leetcode题解 Generate Parentheses的更多相关文章
- [LeetCode 题解]: Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- leetcode之 Generate Parentheses
题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- 安装spy-debugger查看前端日志
有时需要查看前端页面日志,但是前端同学有时忘记开启vConsole.为了调试方便,最好在本地可以查看前端日志,做到一劳永逸. 1.安装node 网上搜教程 2.安装spy-debugger sudo ...
- HBase - Filter - 过滤器的介绍以及使用
1 过滤器HBase 的基本 API,包括增.删.改.查等.增.删都是相对简单的操作,与传统的 RDBMS 相比,这里的查询操作略显苍白,只能根据特性的行键进行查询(Get)或者根据行键的范围来查询( ...
- python urlib2报错gaierror: [Errno 11004] getaddrinfo failed
gaierror : get address info error,获取网络地址信息错误. url不正确,代理信息配置不正确都会报这个错误. 摘自https://blog.csdn.net/qq_19 ...
- Socket传输简单的信息以及粘包问题的解决
一.简单的socket程序——传输简短文字: # -*- coding: utf-8 -*- # -*- Author: WangHW -*- import socket whw_client = s ...
- [UE4]加入音效
- windows下安装、卸载mysql服务
将下载下来的mysql解压到指定目录下(如:d:\mysql)安装服务在命令行输入d:\mysql\bin\mysqld -installnet start mysql卸载服务在命令行输入net st ...
- C#语言基础知识
一. string i_str = "321"; string 型,强制转换成 int 型 int i_int1 = int.parse(i_str); string 型,强制转换 ...
- (转)程序员级别鉴定书(.NET面试问答集锦)
原文地址:http://www.cnblogs.com/powertoolsteam/p/what-net-developer-should-to-know.html 葡萄城控件 作为一个.NET程序 ...
- 【Unix网络编程】chapter8基本UDP套接字编程
chapter8基本UDP套接字编程 8.1 概述 典型的UDP客户端/服务端的函数调用 8.2 recvfrom和sendto函数 #include <sys/socket.h> ssi ...
- KVM总结-KVM性能优化之网络性能优化
前面已经介绍了KVM CPU优化(http://blog.csdn.net/dylloveyou/article/details/71169463).内存优化(http://blog.csdn.net ...