当前位置:网站首页>[LeetCode]20. Valid parentheses thirty-six
[LeetCode]20. Valid parentheses thirty-six
2022-06-13 00:12:00 【PowerDon】
Given one only includes ‘(’,’)’,’{’,’}’,’[’,’]’ String , Determines whether the string is valid .
Valid string needs to meet :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order .
Note that an empty string can be considered a valid string .
Example 1:
Input : “()”
Output : true
Example 2:
Input : “()[]{}”
Output : true
Example 3:
Input : “(]”
Output : false
Example 4:
Input : “([)]”
Output : false
Example 5:
Input : “{[]}”
Output : true
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/valid-parentheses
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
public class Solution {
public bool IsValid(string s) {
if(s.Length == 0){
return true;
}
if(Array.IndexOf(new char[]{
')',']','}'}, s[0]) >= 0){
return false;
}
Stack map = new Stack();
foreach(char ch in s){
if(Array.IndexOf(new char[]{
'(','[','{'}, ch) >= 0){
map.Push(ch);
}else{
if(map.Count == 0){
return false;
}
if(Math.Abs(ch - (char)map.Peek()) <= 2){
map.Pop();
}else{
return false;
}
}
}
return map.Count == 0 ? true : false;
}
}
边栏推荐
- 如何让矢量瓦片配图神器maputnik支持 geoserver
- Divicon est toujours utilisé dans le leaflet de l'ère H5?
- 你真的会用PostGIS中的buffer缓冲吗?
- 机加工行业MES系统模具行业MES系统CNCl中工行业MES系统MES扫码报工MES数据采集
- [matlab] 3D curve and 3D surface
- Case sharing of online real queuing system reconfiguration -- practical part
- Interprocess communication - shared memory shmat
- How to make maputnik, a vector tile matching artifact, support GeoServer
- Cherry Blossom powder Dudu
- Summary of individual NLP internship experience
猜你喜欢

What are the levels of safety accidents

The PMP examination time in March 2022 is set -- "March 27"

Explain bio, NiO, AIO in detail

Basic operations of dict and set

How to gracefully solve the offset problem of Baidu and Gaode maps in leaflet

Is the PMP training organization an actual training?

Learn to divide subnets in an article

Basics of network security (1)

KAUST:Deyao Zhu | 价值记忆图:基于离线强化学习的图结构世界模型

Will the salary increase after obtaining PMP certification?
随机推荐
2022 beautician (technician) certificate title and answer
【Matlab】多项式计算
How to publish OSM maps locally and customize the mapping
[hcie discussion] multicast igmp-a
Interprocess communication - shared memory shmat
【Matlab】基础知识
Free lottery --- PMP renewal PDU | PMP knowledge map
KConfig
How leaflet gracefully displays the bubble window of overlapping points
PMP registration conditions, time, cost, new version related information
Can branches sign labor contracts with employees
哲学和文学的区别
[matlab] two dimensional curve
在 Golang 中构建 CRUD 应用程序
2022 constructeur - direction de l'équipement - Fondation générale (constructeur) Questions d'examen du certificat d'exploitation et examen de simulation
Huawei cloud elastic ECS use [Huawei cloud to jianzhiyuan]
【Matlab】三维曲线与三维曲面
Kaust:deyao Zhu | value memory map: a graph structured world model based on off-line reinforcement learning
Machining Industry MES system Mold Industry MES system CNCl Medium Industry MES System MES code scanning and reporting MES data collection
Online examination questions for September examination of financial management