【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 ...
随机推荐
- 【风马一族_C】c语言版,在2到n中寻找出所有的素数
#include <iostream> #include <stdio.h> #include <math.h> /* run this program using ...
- [视频]ARM告诉你物联网怎么玩,mbed 6LoWPan demo
该视频演示了基于arm mbed的物联网设备间的6LoWPAN应用,如连接家里的土壤湿度传感器,灯光控制,安防联动等应用. 演示视频 原创文章,转载请注明: 转载自 http://www. ...
- android自定义控件实现TextView按下后字体颜色改变
今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能 直接看图片 第一张是按下后截的图,功能很简单, ...
- Python pass 语句使用示例
Python pass 语句的使用方法示例.Python pass是空语句,pass语句什么也不做,一般作为占位符或者创建占位程序,是为了保持程序结构的完整性,pass语句不会执行任何操作,比如: P ...
- 小鸟哥哥博客 For SAE
独立博客地址:http://www.zhujiawei.com.cn/ 辞职后出去玩了几个月,把积蓄都快花光了,打算熬到年底再找工作.最近闲来无聊,想起自己一年前趁着活动便宜,一口气买了10年的域名一 ...
- Lua 的函数库 01
这里只介绍和插件编写比较有关的几个函数. 详细的Lua手册请参照Lua Reference Manual 5.1. table函数库 一部分的table函数只对其数组部分产生影响, 而另一部分则对整个 ...
- CKeditor的简单使用
由于项目中要使用ckeditor 做个推荐功能,由于值设定到文本内容,就选择最基本的使用. 使用的版本为当前最新版本4.4.7,你需要下载两部分,一个是前台使用,一个是后台使用, 你可以到我的网盘中下 ...
- paypal api 相关资料
https://developer.paypal.com/ https://developer.paypal.com/docs/classic/api/merchant/GetBalance_API_ ...
- 第六章Linux的文件权限与目录配置
一.Linux用户分类 1.Linux用户分为:(文件|目录)所有者(OWN),(同组内的)用户组,其他人; 2.一个天神:root;,几乎能完成任何事.... 二.目录权限的意义 目录的权限和文件的 ...
- 2016/09/21 java关键字static
1.static方法 static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上th ...