[Leetcode] 20. Valid Parentheses(Stack)
括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配。代码如下:
class Solution {
public:
bool isValid(string s) {
stack<char> T;
for(int i = ;i < s.length();i ++)
{
if((T.empty()) || (s[i] == T.top()) || abs(s[i] - T.top()) > )
{
T.push(s[i]);
}
else T.pop();
}
if(T.empty())
return true;
else
return false;
}
};
[Leetcode] 20. Valid Parentheses(Stack)的更多相关文章
- 20. Valid Parentheses(stack)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- 32. Longest Valid Parentheses (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- ABAP术语-Document Number
Document Number 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/28/1055636.html Key which ident ...
- java 代码调用函数
sql 中调用函数 getDistance(lng1 float, lat1 float, lng2 float, lat2 float) 例如: SELECT id, f_seller_id sel ...
- Spark知识点
1.Spark架构 分布式spark应用中的组件 在分布式环境下,Spark集群采用的是主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器 ...
- Vue简单使用,
一些零碎的知识点: 在js中变量的声明 有三种方式: let,var, const let: 对应的是一个块级作用域 { let a = 12 } console.log(a) 这是未声明的, var ...
- Leecode刷题之旅-C语言/python-101对称二叉树
/* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...
- 解决 Python2 和 Python3 的共存问题
首先安装两种版本的Python 进入系统属性更改环境变量 将两个版本的安装路径找出. 添加至PATH中,变量之间用分号隔开. D:\Python36\Scripts\;D:\Python36\;D:\ ...
- go学习笔记-Data类型(Arrays, Slices and Maps)
Data类型(Arrays, Slices and Maps) array array就是数组,定义方式如下: var arr [n]type 在[n]type中,n表示数组的长度,type表示存储元 ...
- 分布式redis一些小结
本文围绕以下几点进行阐述: 为什么使用 Redis 使用 Redis 有什么缺点 单线程的 Redis 为什么这么快 Redis 的数据类型,以及每种数据类型的使用场景 Redis 的过期策略以及内存 ...
- 保证IO流不出错
package com.io.demo1; import java.io.FileInputStream;import java.io.IOException; /** * 测试IO * io流,输入 ...
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
代码如下: package com.cn.util; import java.io.BufferedInputStream; import java.io.File; import java.io.F ...