[刷题] 20 Valid Parentheses
要求
- 给定一个只包括
'(',')','{','}','[',']'的字符串,判断字符串是否有效 - 左括号必须用相同类型的右括号闭合
- 左括号必须以正确的顺序闭合
- 空字符串可被认为是有效字符串
思路
- 遇到左方向括号入栈,遇到右方向括号查看栈顶元素,匹配则出栈
- 遍历结束后栈为空则满足匹配原则

实现
1 #include<iostream>
2 #include<stack>
3 #include<cassert>
4
5 using namespace std;
6
7 class Solution{
8 public:
9 bool isValid(string s){
10
11 stack<char> stack;
12 for( int i = 0 ; i < s.size() ; i ++ ){
13 if( s[i] == '(' || s[i] == '{' || s[i] == '[')
14 stack.push( s[i] );
15 else{
16
17 if( stack.size() == 0)
18 return false;
19
20 char c = stack.top();
21 stack.pop();
22
23 char match;
24 if( s[i] == ')')
25 match = '(';
26 else if( s[i] == ']' )
27 match = '[';
28 else{
29 assert( s[i] == '}' );
30 match = '{';
31 }
32
33 if( c != match)
34 return false;
35 }
36 }
37
38 if( stack.size() != 0 )
39 return false;
40
41 return true;
42 }
43 };
44
45 void printBool(bool res){
46 cout << (res ? "True" : "False") << endl;
47 }
48
49 int main(){
50
51 printBool(Solution().isValid("()"));
52 printBool(Solution().isValid("()[]{}"));
53 printBool(Solution().isValid("(]"));
54 printBool(Solution().isValid("([)]"));
55
56 return 0;
57 }

[刷题] 20 Valid Parentheses的更多相关文章
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 乘风破浪:LeetCode真题_032_Longest Valid Parentheses
乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- 敏捷史话(十二):你现在接触的敏捷也许是“黑暗敏捷”——Ron Jeffries
他很少提起往事,也不再提及二十年前那场引起软件行业变革的会议,他专注于当下,一直活跃在敏捷领域.八十多岁的他依然运营维护着网站和博客,是极限编程网站 XProgramming.com 的作者,该网站是 ...
- HTML5本地存储 localStorage操作使用详解
1.html5几种存储形式 本地存储(localStorage && sessionStorage) 离线缓存(application cache) indexedDB 和 webSQ ...
- lms框架分布式事务使用简介
lms框架的分布式事务解决方案采用的TCC事务模型.在开发过程中参考和借鉴了hmily.使用AOP的编程思想,在rpc通信过程中通过拦截器的方式对全局事务或是分支事务进行管理和协调. 本文通过lms. ...
- Vulkan移植GpuImage(三)从A到C的滤镜
前面移植了几个比较复杂的效果后,算是确认了复杂滤镜不会对框架造成比较大的改动,开始从头移植,现已把A到C的所有滤镜用vulkan的ComputeShader实现了,讲一些其中实现的过程. Averag ...
- house_of_storm 详解
house_of_storm 漏洞危害 House_of_storm 可以在任意地址写出chunk地址,进而把这个地址的高位当作size,可以进行任意地址分配chunk,也就是可以造成任意地址写的后果 ...
- 05_pytorch的Tensor操作
05_pytorch的Tensor操作 目录 一.引言 二.tensor的基础操作 2.1 创建tensor 2.2 常用tensor操作 2.2.1 调整tensor的形状 2.2.2 添加或压缩t ...
- Vue3+Vite引入Echarts5.0图表库
1 概述 环境Vue3+Vite,需要引入ECharts库. 2 尝试 目前ECharts已更新到5.0版本,在Vue中引入并不难,npm/cnpm安装后在需要的组件中引入: import echar ...
- JavaScript实现斐波那契数列
什么是斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为&qu ...
- python3使用迭代生成器yield减少内存占用
技术背景 在python编码中for循环处理任务时,会将所有的待遍历参量加载到内存中.其实这本没有必要,因为这些参量很有可能是一次性使用的,甚至很多场景下这些参量是不需要同时存储在内存中的,这时候就会 ...
- 1.5.1- HTML之相对路径
网页需要图片,首先需要找到它.实际工作中,通常新建一个文件夹专门用于存放图像文件,这时插入图像,就需要采用"路径"的方式来制定图像文件的位置.路径可以分为相对路径与绝对路径. 相对 ...