当前位置:网站首页>Code007 -- determine whether the string in parentheses matches
Code007 -- determine whether the string in parentheses matches
2022-04-23 04:39:00 【singularityDZF】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* 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
*
*/
public class test07 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = bf.readLine();
//System.out.println(s.length());
System.out.println(s+" Whether it works :"+isValid(s));
}
// public static boolean isValid(String s){
// boolean flag = false;
// for( int i=(s.length()/2)-1; i >= 0; i--){
// int j = 1;
// if ( s.charAt(i) == '(' && s.charAt(i+j) == ')'){
// flag = true;
// j += 2;
// }else if (s.charAt(i) == '[' && s.charAt(i+j) == ']'){
// flag = true;
// j += 2;
// }else if (s.charAt(i) == '{' && s.charAt(i+j) == '}'){
// flag = true;
// j += 2;
// }else{
// flag = false;
// break;
// }
// }
// return flag;
// }
public static boolean isValid(String s){
int n = s.length();
if( n%2 ==1){
return false;
}
Map<Character,Character> map = new HashMap<Character,Character>();
map.put(')', '(');
map.put(']', '[');
map.put('}', '{');
// Judge with the characteristics of stack first in and last out , The current symbol only needs to match the top element of the stack : matching , Stack top element out of stack ; conversely , The current element is put on the stack
Deque<Character> stack = new LinkedList<Character>();
for( int i=0; i<n; i++){
char ch = s.charAt(i);
if(map.containsKey(ch)){
if(stack.isEmpty() || stack.peek() != map.get(ch)){ // Determine whether the stack is empty or the top element of the stack does not match the current element
return false;
}
stack.pop(); // Stack top element out of stack
}else{
stack.push(ch); // There is no current element in the stack , The current element is put on the stack ( To put it bluntly '(','[' and '{' These three symbols are stacked )
}
}
return stack.isEmpty();
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230438035335.html
边栏推荐
- Detailed explanation of life cycle component of jetpack
- 【Echart】echart 入門
- 兼容NSR20F30NXT5G的小体积肖特基二极管
- MySQL queries users logged in for at least N consecutive days
- leetcode004--罗马数字转整数
- MYSQL查询至少连续n天登录的用户
- Bacterial infection and antibiotic use
- Introduction to Cortex-M3 register set, assembly language and C language interface
- leetcode005--原地删除数组中的重复元素
- A heavy sword without a blade is a great skill
猜你喜欢
Interaction of diet gut microbiota on cardiovascular disease
229. 求众数 II
補:注解(Annotation)
Effects of antibiotics on microbiome and human health
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
383. 赎金信
Matlab minimalist configuration of vscode configuration
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
IDE idea automatic compilation and configuration of on update action and on frame deactivation
随机推荐
Nature medicine reveals individual risk factors of coronary artery disease
leetcode001--返回和为target的数组元素的下标
协程与多进程的完美结合
QML advanced (IV) - drawing custom controls
Use recyclerview to realize left-right side-by-side classification selection
VHDL implementation of 32-bit binary to BCD code
Detailed explanation of life cycle component of jetpack
A new method for evaluating the quality of metagenome assembly - magista
补:注解(Annotation)
[paper reading] [3D object detection] voxel transformer for 3D object detection
SQL statement for adding columns in MySQL table
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
顺序表的基本操作
三十六计是什么
A heavy sword without a blade is a great skill
test
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Supplement 14: cmake practice project notes (to be continued 4 / 22)
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer