当前位置:网站首页>Leetcode 20. Valid parentheses
Leetcode 20. Valid parentheses
2022-04-23 20:26:00 【Die in a trap】
20、 Valid parenthesis
1) Title Description
Given one only includes '('
,')'
,'{'
,'}'
,'['
,']'
String s
, 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 .
Example 1:
Input :s = "()"
Output :true
Example 2:
Input :s = "()[]{}"
Output :true
Example 3:
Input :s = "(]"
Output :false
Example 4:
Input :s = "([)]"
Output :false
Example 5:
Input :s = "{[]}"
Output :true
Tips :
1 <= s.length <= 10^4
s
Brackets only'()[]{}'
form
2) analysis
Take advantage of the first in first out feature of the stack , Stack to solve the problem .
- Parentheses complete pairing , Then the string must be even length ;
- Traversal string , If it's left bracket , Then press the corresponding right parenthesis into the stack ;
- If it's a right parenthesis , Determine whether the characters at the top of the stack are equal to them , If not equal , return
false
; - End of traversal , If the stack is empty , return
true
, Otherwise return tofalse
.
3)C++
Code
class Solution {
public:
bool isValid(string s) {
if(s.length()%2)
return false;
stack<int> stk;
for(int i=0;i<s.length();i++){
if(s[i]=='(')
stk.push(')');
else if(s[i]=='[')
stk.push(']');
else if(s[i]=='{')
stk.push('}');
else if(stk.empty()||stk.top()!=s[i])
return false;
else
stk.pop();
}
return stk.empty();
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107456.html
边栏推荐
- ABAQUS script email auto notification
- 內網滲透之DOS命令
- Paper writing 19: the difference between conference papers and journal papers
- R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
- Monte Carlo py solves the area problem! (save pupils Series)
- Numpy mathematical function & logical function
- Browser - learning notes
- Common form verification
- After route link navigation, the sub page does not display the navigation style problem
- Matlab analytic hierarchy process to quickly calculate the weight
猜你喜欢
考研英语唐叔的语法课笔记
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
Computing the intersection of two planes in PCL point cloud processing (51)
Modeling based on catiav6
[PTA] get rid of singles
16MySQL之DCL 中 COMMIT和ROllBACK
随机推荐
【栈和队列专题】—— 滑动窗口
Automatically fill in body temperature and win10 task plan
Experience of mathematical modeling in 18 year research competition
LeetCode 1346、检查整数及其两倍数是否存在
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution
Sqoop imports tinyint type fields to boolean type
R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
Numpy mathematical function & logical function
How does onlyoffice solve no route to host
Mysql database backup scheme
Matlab analytic hierarchy process to quickly calculate the weight
How can matlab obtain the truncated image in trainingimagelabeler
star
R language uses the preprocess function of caret package for data preprocessing: BoxCox transform all data columns (convert non normal distribution data columns to normal distribution data and can not
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
Commande dos pour la pénétration de l'Intranet
Vscode download speed up