【Leetcode】【Easy】Valid Parentheses
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.
解题思路:
栈使用的典型题目;
解题步骤:
1、新建一个栈;
2、遍历字符串:
(1)符号的左半边,则入栈;
(2)符号的右半边,则pop出栈顶元素,如果栈为空,则返回false;
判断此右半边和栈顶元素是否匹配;匹配继续;不匹配则返回false;
3、循环结束后栈为空,则true,不为空则false;
class Solution {
public:
bool isValid(string s) {
stack<char> st;
int len = s.length();
for(int i = ; i < len; ++i) {
if (s[i] == ')' || s[i] == ']' || s[i] == '}') {
if (st.empty())
return false;
char c = st.top();
if ((c == '(' && s[i] != ')') || \
(c == '[' && s[i] != ']') || \
(c == '{' && s[i] != '}'))
return false;
st.pop();
} else {
st.push(s[i]);
}
}
return st.empty();
}
};
附录:
常见符号ASCII码
C++常见容器使用及公式
【Leetcode】【Easy】Valid Parentheses的更多相关文章
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- VMware下安装CentOS7 无法通过桥接模式进行联网
#修改配置# vi /etc/sysconfig/network-scripts/ifcfg-ens33 (ifcfg-ens33,不同的机器名称不一样,但大都为ifcfg-ensxxx) 把文件中 ...
- JavaScript笔记Array.filter(Boolean)
ECMAScirpt5 中 Array 类中的 filter 方法使用目的是移除所有的 ”false“ 类型元素 (false, null, undefined, 0, NaN or an empt ...
- VBS修改本机的账号密码
On Error Resume Next strComputer = "." Set WshShell = WScript.CreateObject("WScript.S ...
- springboot(八)-定时任务
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容. 如果我们不用springboot开发的话,我们写定时任务需要写那些配置呢? 我们需要在application.xml文件中添加以下配置 ...
- 给JFinal添加 Sqlite 数据库支持
[转自] http://my.oschina.net/u/237078/blog/69934 Sqlite 的单文件便携性.高性能在开发中方便性无与伦比,即使部署在中小型应用中也胜任有余. 在JFin ...
- 爬虫之re块解析
一.re 这个去匹配比较麻烦,以后也比较少用,简单看一个案例就行 ''' 爬取数据流程: 1.指定url 2.发起请求 3.获取页面数据 4.数据解析 5.持久化存储 ''' import reque ...
- 坐标&接龙型动态规划 - 20181026
109. Triangle 此题还可以用DFS,记忆化搜索去做,二刷实现 public class Solution { /** * @param triangle: a list of lists ...
- Linux mmap 要主动释放共享内存
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ...
- PIXI 宝物猎人(7)
介绍 ,本实例来自官网 代码结构 打开 treasureHunter.html 文件,你将会看到所有的代码都在一个大的文件里.下面是一个关于如何组织所有代码的概览: //Setup Pixi and ...
- SEO艺术阅读笔记
SEO(Search Engine Optimization)搜索引擎优化 搜索引擎:反映认知,连接贸易搜索引擎基础百度搜索高级语法确定SEO目标,定义网站受众设定SEO目标开发前定制SEO方案理 ...