输入描述:
给定一个字符串S,请检查该字符串的括号是否配对,只含有"[", "]", "(", ")"
输出描述:
配对,返回true
不配对,返回false
示例1
输入
abcd(])[efg
输出
false
解题思路:用栈来解题,遍历字符串,遇到(、[ 入栈,如果遇到 ‘)’、‘ ] ’,查看栈顶元素是否配对(),[ ]。如果配对就出栈,否则就报错。还有一点,遍历结束后,还要检查栈是否为空栈。
import java.util.Scanner;
import java.util.Stack;
public class Test0629 {
public static void main(String[] args) {
Stack<Character> stack = new Stack();
Scanner in = new Scanner(System.in);
String str = in.nextLine();
for(int i = 0;i<str.length();i++){
if(str.charAt(i) == '(' || str.charAt(i) == '['){
stack.push(str.charAt(i));
}else if(str.charAt(i) == ')' ){
if( !stack.empty() && stack.peek() == '(' ){
stack.pop();
}else{
System.out.println(false);
return ;
}
}else if(str.charAt(i) == ']'){
if(!stack.empty() && stack.peek() == '['){
stack.pop();
}else{
System.out.println(false);
return ;
}
}
}
System.out.println(stack.empty());
}
}
219

被折叠的 条评论
为什么被折叠?



