当前位置:网站首页>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
边栏推荐
- SQL statement for adding columns in MySQL table
- zynq平台交叉编译器的安装
- 520.检测大写字母
- Installation du compilateur croisé de la plateforme zynq
- 无线键盘全国产化电子元件推荐方案
- AWS EKS添加集群用户或IAM角色
- Go反射—Go语言圣经学习笔记
- Effects of antibiotics on microbiome and human health
- [paper reading] [3D target detection] point transformer
- Summary of MySQL de duplication methods
猜你喜欢

Chapter 4 - understanding standard equipment documents, filters and pipelines
![[paper reading] [3D target detection] point transformer](/img/c5/b1fe5f206b5fe6e4dcd88dce11592d.png)
[paper reading] [3D target detection] point transformer

Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library

Use recyclerview to realize left-right side-by-side classification selection

递归调用--排列的穷举

Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation

C语言:恶搞小游戏

Recursive call -- Enumeration of permutations

Bacterial infection and antibiotic use

Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
随机推荐
Recursive call -- Enumeration of permutations
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
补充番外14:cmake实践项目笔记(未完待续4/22)
三十六计是什么
Differences among electric drill, electric hammer and electric pick
Mysql50 basic exercises
RC低通滤波器的逆系统
leetcode002--将有符号整数的数字部分反转
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Create VPC in AWS console (no plate)
SQL statement for adding columns in MySQL table
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
Eksctl deploying AWS eks
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
520. Detect capital letters
AWS EKS添加集群用户或IAM角色