当前位置:网站首页>[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;
}
}
边栏推荐
- How does the PMP handle the withdrawal?
- 哲學和文學的區別
- [matlab] two dimensional curve
- Video tracker error troubleshooting
- TypeError: wave.ensureState is not a function
- Leaflet that supports canvas Path. Dashflow dynamic flow direction line
- How to visit a website
- Accelerating with Dali modules
- What can PMP bring to you
- 浏览器缓存的执行流程
猜你喜欢
MASA Auth - 从用户的角度看整体设计
Free lottery --- PMP renewal PDU | PMP knowledge map
PMP renewal | PDU specific operation diagram
浏览器缓存的执行流程
Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity
Running of NCF dapr application instance
机加工行业MES系统模具行业MES系统CNCl中工行业MES系统MES扫码报工MES数据采集
63. different paths II
Tsinghua-Bosch Joint ML Center, THBI Lab:Chengyang Ying | 通过约束条件风险价值实现安全强化学习
Will the salary increase after obtaining PMP certification?
随机推荐
C language standard IO, for example: fread(), fwrite(), fgetc(), etc. (end)
Online examination questions for September examination of financial management
启牛商学院里面的券商账户是安全的吗?开户费率低吗
Why study PMP?
Summary of individual NLP internship experience
Pytorch loading model error resolution
Actual combat | UI automation test framework design and pageobject transformation
Cherry Blossom powder Dudu
The e-commerce employee changed the product price to 10% off after leaving the company, and has been detained
The most complete preview! Huawei cloud wonderful agenda collection
如何让矢量瓦片配图神器maputnik支持 geoserver
Divicon est toujours utilisé dans le leaflet de l'ère H5?
PMP test difficulty and pass rate
分公司能与员工签劳动合同么
PLC can also make small games ----- CoDeSys can write small games of guessing numbers
哲學和文學的區別
Explain bio, NiO, AIO in detail
电商员工离职后将产品价格改为1折出售,已被刑拘
【Matlab】二维曲线
C # graphic tutorial (Fourth Edition) chapter7-7.6.1 virtual and override