【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 ...
随机推荐
- CodeForces - 1110E-Magic Stones(差分+思维)
Grigory has nn magic stones, conveniently numbered from 11 to nn. The charge of the ii-th stone is e ...
- 119th LeetCode Weekly Contest Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- Win10如何新建用户怎么添加新账户
https://jingyan.baidu.com/article/25648fc162d5899190fd0069.html 很多朋友都是安装完Windows10系统后,直接使用超级管理员账号登录系 ...
- 配intelliJ IDEA 过程
1.安装svn 选项全选择,命令行执行要选择上2.安装java jdk 配jdk环境变量3.安装intelliJ IDEA 地址:http://idea.imsxm.com4.注册intelliJ I ...
- STL:vector用法总结
一:介绍 vector是C++标准模板库,是一个容器,底层是数组,为连续内存.命名空间为std,所属头文件为<vector> 注意:不是<vector.h>vector存储 ...
- Android的Intent和IntentFilter应用说明一例
很多人对文档中的Intent和IntentFilter不理解是什么意思,我这里举例解释下. Intent字面意思就是目标,目的.通俗一点,需要达成某些目标,则需要提供一些动作,这些目标的分类,以及达成 ...
- 【c#文档】在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
[c#文档]https://msdn.microsoft.com/zh-cn/library/system.convert.toint32.aspx 转载自:http://www.cnblogs.co ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- js实现CkeckBox全选与反选
全选与反选 function SelectAll(){ var check = document.getElementsByTagName("input"); // 获取所有inp ...
- multiprocessing 模块
multiprocessing模块 进程对象 创建 p = Process(target=foo, args=(param,)) 属性 p.daemon: True为守护进程, 守护进程内无法再开启子 ...