【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++对象模型以及内存布局的研究
先引出问题,看一段代码: #include <iostream> using namespace std; class A { }; class B { public: B() {} ~B ...
- 深入了解Javascript模块化编程
本文译自Ben Cherry的<JavaScript Module Pattern: In-Depth>.虽然个人不太认同js中私有变量存在的必要性,但是本文非常全面地介绍了Javascr ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- 自定义Toast的显示效果
Activity: package com.example.editortoast; import android.app.Activity; import android.os.Bundle; im ...
- 禁止 PC端打开网页 进行跳转
try {var urlhash = window.location.hash;if (!urlhash.match("fromapp")){if ((navigator.user ...
- Windows Server 2003开机自动启动MySQL服务设置方法
Windows Server 2003开机自动启动MySQL服务设置方法 发布时间:2014-12-19 更新时间:2014-12-24 来源:网络 作者:eaglezhong 关键词: 2003 e ...
- Jquery在项目中的总结
1.构造对象 var _getSearchArg = function () { var argModel = {}; argModel.Txt = value; argModel.Code = va ...
- 【转载!】关于C#的RawSocket编程的问题
Q:你好! 看过了你在csdn上发表的<用C#下的Raw Socket编程实现网络封包监视>,觉得很感兴趣,而且对我的帮助很大.不过在调试的过程中遇到一些问题,特此向你请教一下.谢谢! 首 ...
- 线程操作API
线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...
- unix的策略与机制
策略同机制分离,接口同引擎分离 Linux/Unix设计理念提供的一种机制不是策略.如果说机制是一种框架,那么,策略就是填充框架的一个个具体实施.机制提供的就是一种开放而宽松的环境,而策略就是在这个环 ...