leetcode第20题--Valid Parentheses
Problem:
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.
判断输入的括号是不是合法。这题在大二的时候就遇到过了。想不到我的记性这么好。因为当时我什么都不懂,刚学数据结构,然后有个ACMer是助教,他给我讲解了这题。然后我就一直都记得了。也不知道那个师兄现在在哪里牛逼。
回到题目,就是用栈实现。
class Solution {
private:
char anti(char c)
{
if (c == ']')
return '[';
if (c == ')')
return '(';
if (c == '}')
return '{';
}
public:
bool isValid(string s) {
int len = s.size();
if (len%)
{ return false;}
if(s.size() == || s[] == ')' || s[] == ']' || s[] == '}')
return false;
stack<char> st;
for (int i = ; i < len; i++)
{
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
st.push(s[i]);
else
if (st.top() == anti(s[i]))
st.pop();
else
return false;
}
if (st.empty())
return true;
else
return false;
}
};
代码写得有点挫,大概就是这个思路,然后Accept了
leetcode第20题--Valid Parentheses的更多相关文章
- 【LeetCode算法-20】Valid Parentheses
LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- LeetCode(20)Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- Leetcode 题目整理-5 Valid Parentheses & Merge Two Sorted Lists
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
随机推荐
- Android开发技巧——实现在图标文本底部导航栏(更新)
本文参考了导航栏的代码viewpagerindicator实现. 本文介绍了之前版本号qq或者微信,添加文本,实现图标,导航栏的底部. 2014-09-14 13:59:42更新:library的代码 ...
- SQL入门学习3-数据更新
4-1 数据的插入(INSERT语句的使用方法) 使用INSERT语句可以向表中插入数据(行).原则上,INSERT语句背刺执行一行数据插入. CREATE TABLE 和INSERT 语句,都可以设 ...
- NSIS脚本:更改壁纸
原文 NSIS脚本:更改壁纸 我们在制作主题安装包的时候,经常要进行自动更改壁纸的操作,其实用NSIS实现这一点非常简单.示例代码如下: 01 Name "更改壁纸" 02 Out ...
- JAVA学习笔记 -- JDBC及其应用
一个.准备工作 1.开放SQL Server服务与支持TCP/IP 进一步确认TCPport watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk ...
- 在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table)
原文:在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table) 打印指定内容: <html> <head> <script type= " ...
- react学习笔记2--练习Demos
准备工作 # 0.react核心库 <script src="../build/react.js"></script> # 将JSX 语法转为 JavaSc ...
- 使用Maven在Eclipse中创建Web项目[转]
一.新建 Maven Web项目 1.新建Maven Project new project-->选择 Maven Project --> 下一步 选择工作空间 -->下一步 在Fi ...
- Jenkins + robot framework + git持续集成
安装略过... 一.Jenkins安装插件 进入系统管理—插件管理—可选插件下安装以下插件Git Client Plugin.GIT plugin.GitHub API Plugin.GitHub p ...
- webkit 子资源加载过程
从主控文档和子资源表单的页面.描述框架记叙文页主文档,布局.子元素.包含图片.CSS.JS等.为了显示网页,先要把资源载入到内存. 载入就是指把须要的资源载入到内存这一过程. Webkit用到非常多缓 ...
- 【百度地图API】建立全国银行位置查询系统(四)——如何利用百度地图的数据生成自己的标注
原文:[百度地图API]建立全国银行位置查询系统(四)--如何利用百度地图的数据生成自己的标注 摘要: 上一章留个悬念,"如果自己没有地理坐标的数据库,应该怎样制作银行的分布地图呢?&quo ...