当前位置:网站首页>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^4sBrackets 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
边栏推荐
- Go zero framework database avoidance Guide
- Browser - learning notes
- bounding box iou
- WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
- Numpy Index & slice & iteration
- [graph theory brush question-4] force deduction 778 Swimming in a rising pool
- C migration project record: modify namespace and folder name
- BMP JPEG picture to vector image contourtrace
- Handwritten Google's first generation distributed computing framework MapReduce
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
猜你喜欢

Vscode download speed up

go-zero框架数据库方面避坑指南

LeetCode 994、腐烂的橘子

. Ren -- the intimate artifact in the field of vertical Recruitment!

Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference

Scrapy教程 - (2)寫一個簡單爬蟲

Commande dos pour la pénétration de l'Intranet

PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)

Devexpress 14.1 installation record

Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
随机推荐
LeetCode 542、01 矩阵
Paper writing 19: the difference between conference papers and journal papers
Identification of bolt points in aerial photography based on perception
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
Automatically fill in body temperature and win10 task plan
Experience of mathematical modeling in 18 year research competition
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
Vscode download speed up
PCL点云处理之直线与平面的交点计算(五十三)
Investigate why close is required after sqlsession is used in mybatties
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
PCL点云处理之基于PCA的几何形状特征计算(五十二)
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
Linux64Bit下安装MySQL5.6-不能修改root密码
Research on open source OCR engine
Numpy mathematical function & logical function
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
Servlet learning notes