【Valid Parentheses】cpp
题目:
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 "([)]" are not.
代码:
class Solution {
public:
bool isValid(string s) {
std::stack<char> result;
for ( size_t i = ; i < s.length(); ++i )
{
if ( s[i]=='(' || s[i]=='[' || s[i]=='{' )
{
result.push(s[i]);
continue;
}
if ( result.empty() ) return false;
char top = result.top();
if ( s[i]==')' && top!='(' ) return false;
if ( s[i]==']' && top!='[' ) return false;
if ( s[i]=='}' && top!='{') return false;
result.pop();
}
return result.empty();
}
};
tips:
口诀:左号进栈,右号出栈;出栈判断是否正确。
===========================================
第二次过这道题,凭感觉写了一份代码,稍微冗长一些;修改了一次AC了。
class Solution {
public:
bool isValid(string s) {
stack<char> sta;
int i = ;
while ( i<s.size() )
{
if ( s[i]=='(' || s[i]=='[' || s[i]=='{' )
{
sta.push(s[i]);
}
else if ( s[i]==')' || s[i]==']' || s[i]=='}' )
{
if ( sta.empty() ) return false;
if ( s[i]==')' )
{
if ( sta.top()=='(' )
{
sta.pop();
}
else
{
return false;
}
}
else if ( s[i]==']' )
{
if ( sta.top()=='[' )
{
sta.pop();
}
else
{
return false;
}
}
else
{
if ( sta.top()=='{' )
{
sta.pop();
}
else
{
return false;
}
}
}
else
{
return false;
}
++i;
}
return sta.empty();
}
};
有个地方不要遗漏:如果是右号,要判断一下stack是否为空。
【Valid Parentheses】cpp的更多相关文章
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Valid Palindrome】cpp
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 【Generate Parentheses】cpp
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- 【Valid Number】cpp
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Sudoku Solver】cpp
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 【Gray Code】cpp
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
随机推荐
- DPDK多核多线程机制简析
DPDK通过在多核设备上,创建多个线程,每个线程绑定到单独的核上,减少线程调度的开销,以提高性能. DPDK的线程分为控制线程和数据线程,控制线程一般绑定到MASTER核上,主要是接受用户配置,并传递 ...
- 文本分析工具awk简单示例
先创建一个文件:vim hi 取第2个字段和第3个字段: awk '{print $2,$3}' hi 注意{}中的,逗号会在输出的时候转变为空格 加入字符说明: 显示整行: 指定字段分隔符: ...
- 最近的学习的linux命令笔记
vmstat 2 2 mail -s nihao root < test.txt f 323 h 300-310 crontab -l,-e,-r vim /etc/cron ...
- ajax+php+mysql更新
html代码 <input type="button" id="quxiao" class="quxiao" name="q ...
- 傅里叶变换 fft_generic halcon
傅立叶变换(FT, Fourier Transform)的作用是将一个信号由时域变换到频域.其实就是把数据由横坐标时间.纵坐标采样值的波形图格式,转换为横坐标频率.纵坐标振幅(或相位)的频谱格式.变换 ...
- CentOS 7上的redis搭建
http://www.cnblogs.com/shanyou/archive/2012/07/14/2591881.html
- 共享内存shared pool (5):详解一条SQL在library cache中解析
前面介绍的 shared pool,library cache结构,都是为了说明一条SQL是如何被解析的.先看下面的图: 图中涉及的各结构简单介绍 父HANDLE,里面有父游标堆0的地址.. 父游标堆 ...
- 解决matplotlib中文乱码问题(Windows)
1.修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.fam ...
- 浅谈objective—C管理内存
这段时间被导师催着论文,调试各种BUg,也是醉了,发现很大程度上,内存出错,栈溢出,各种悲剧.那么今天就和大家一起对OC的内存管理来个探微吧.Objective-C使用一个保留计数记录了我们所创建的所 ...
- AsyncTask和Handler两种异步方式的实现和区别比较
1 AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以 ...