当前位置:网站首页>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)
边栏推荐
- Go tool cli for command line implementation
- Feign remote call and getaway gateway
- JS日常开发小技巧(持续更新)
- shell脚本使用两个横杠接收外部参数
- # 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- 世界上最好的学习法:费曼学习法
- 实战 ELK 优雅管理服务器日志
- Introduction and basic knowledge of machine learning
- 【日常训练】1175. 质数排列
- Detailed list of errors related to twincat3 ads of Beifu
猜你喜欢

实战 ELK 优雅管理服务器日志

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS

C#实现图的深度优先遍历--非递归代码

8 pits of redis distributed lock

别再说不会解决 “跨域“ 问题啦

Feign远程调用和Getaway网关

Mysql知识点

Listener listener

Filter

The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
随机推荐
GCC usage, makefile summary
split(),splice(),slice()傻傻分不清楚?
Subnet division and subnet summary
文件上传下载
[exsi] transfer files between hosts
HTB-Lame
How to determine the progress bar loaded in the loading interface when opening the game
4、【WebGIS实战】软件操作篇——数据导入及处理
实现pow(x,n)函数
EtherCAT简介
Latest interface automation interview questions
multiple linear regression
mybati sql 语句打印
Best used trust automation script (shell)
[us match preparation] complete introduction to word editing formula
gcc使用、Makefile总结
Introduction to ieda right click source file menu
不用加减乘除实现加法
Feign remote call and getaway gateway
服务器渲染技术jsp