当前位置:网站首页>Valid brackets (force deduction 20)
Valid brackets (force deduction 20)
2022-07-01 03:26:00 【Jade faced Dragon】
Catalog
Two 、 Train of thought explanation
3、 ... and 、Java Code implementation
5、 ... and 、 Time and space complexity analysis
One 、 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 <= 104
s Brackets only '()[]{}' form
Two 、 Train of thought explanation
The problem of valid bracket classes can basically be solved by stack , Because the matching order of parentheses is just in line with the stack's first in, last out feature .
Traversal string , When you have an open parenthesis , It into the stack ; When you encounter a close bracket , Just pop up , If the pop-up bracket does not match the current right bracket , It means something is wrong .
After traversing the string , If the stack is not empty , It means that there is a left parenthesis , Description error ; If the stack is empty , The match is correct .
It can also be simplified : If you encounter a left bracket , Just press the corresponding right bracket ( For example, meet '(', Just press in ')' ). When you encounter the closing parenthesis, you only need to pop up the element , Just compare whether the pop-up element is consistent with the current bracket , In this way, a few if Judge .
3、 ... and 、Java Code implementation
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()){
if(c=='('){
stack.push(')');
} else if(c=='['){
stack.push(']');
} else if(c=='{') {
stack.push('}');
} else if(stack.isEmpty() || stack.pop()!=c){
return false;
}
}
// If the stack is not empty , It means that there is a left parenthesis ; If the stack is empty , Description brackets are correct
return stack.isEmpty();
}
}
Four 、C++ Code implementation
class Solution {
public:
bool isValid(string s) {
stack<char> stack;
char temp;
for (char ss : s) {
if (ss == '(') {
stack.push(')');
}else if (ss == '[') {
stack.push(']');
}else if (ss == '{') {
stack.push('}');
}else if (stack.empty()) {
return false;
}else if (stack.top() != ss) {
return false;
}else {
stack.pop();
}
}
if (stack.empty()) {
return true;
}else {
return false;
}
}
};5、 ... and 、 Time and space complexity analysis
Time complexity : O(N)
Spatial complexity : O(N)
边栏推荐
- Elk elegant management server log
- 雪崩问题以及sentinel的使用
- Ctfshow blasting WP
- 【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
- MySQL index --01--- design principle of index
- How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
- Redis tutorial
- Detailed explanation of ES6 deconstruction grammar
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- ctfshow爆破wp
猜你喜欢
随机推荐
Avalanche problem and the use of sentinel
EtherCAT原理概述
第03章_用戶與權限管理
A few lines of transaction codes cost me 160000 yuan
倍福TwinCAT3 Ads相关错误详细列表
Split(), split(), slice(), can't you tell?
How to use hybrid format to output ISO files? isohybrid:command not found
ctfshow爆破wp
伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
监听器 Listener
岭回归和lasso回归
串口接收数据方案设计
Include() of array
Common interview questions for performance test
How to determine the progress bar loaded in the loading interface when opening the game
Ridge regression and lasso regression
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
C#实现图的深度优先遍历--非递归代码
别再说不会解决 “跨域“ 问题啦
HTB-Lame







![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)

