import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
/*
*
*
Use a stack to check parentheses, balanced and nesting
* The parentheses are: (), [] and {}
*
* See:
* - UseAStack
*
*/
public class Ex3CheckParen {
public static void main(String[] args) {
new Ex3CheckParen().program();
}
void program() {
// All should be true
out.println(checkParentheses("()"));
out.println(checkParentheses("(()())"));
out.println(!checkParentheses("(()))")); // Unbalanced
out.println(!checkParentheses("((())")); // Unbalanced
out.println(checkParentheses("({})"));
out.println(!checkParentheses("({)}")); // Bad nesting
out.println(checkParentheses("({} [()] ({}))"));
out.println(!checkParentheses("({} [() ({)})")); // Unbalanced and bad nesting
}
// This is interesting because have to return, but what if no match?!?
boolean checkParentheses(String str) {
Deque<Character> stack = new ArrayDeque<>();
String k = "({[";
String s = ")]}";
for (int i = 0; i < str.length(); i++) {
if (k.contains(String.valueOf(str.charAt(i)))) {
stack.push(str.charAt(i));
} else if (s.contains(String.valueOf(str.charAt(i)))) {
if (matching(stack.peek()) == str.charAt(i)) { //ILLEGAL ARGUMENT EXCEPTION HERE
return true;
}
} else {
return false;
}
}
return false;
}
char matching(char ch) {
//char c = must initialize but to what?!
switch (ch) {
case ')':
return '('; // c = '('
case ']':
return '[';
case '}':
return '{';
default:
// return c;
throw new IllegalArgumentException("No match found");
}
}
}
我在包含匹配的if语句中收到异常错误。无法弄清楚原因。
答案 0 :(得分:1)
也许是这样的?
public class Ex3CheckParen {
public static void main(String[] args) {
new Ex3CheckParen().program();
}
void program() {
// All should be true
out.println(checkParentheses("()"));
out.println(checkParentheses("(()())"));
out.println(!checkParentheses("(()))")); // Unbalanced
out.println(!checkParentheses("((())")); // Unbalanced
out.println(checkParentheses("({})"));
out.println(!checkParentheses("({)}")); // Bad nesting
out.println(checkParentheses("({} [()] ({}))"));
out.println(!checkParentheses("({} [() ({)})")); // Unbalanced and bad nesting
}
// This is interesting because have to return, but what if no match?!?
boolean checkParentheses(String str) {
Deque<Character> stack = new ArrayDeque<>();
String k = "({[";
String s = ")]}";
for (int i = 0; i < str.length(); i++) {
if (k.contains(String.valueOf(str.charAt(i)))) {
stack.push(str.charAt(i));
} else if (s.contains(String.valueOf(str.charAt(i)))) {
if (matching(stack.peek(), str.charAt(i))) {
return true;
}
} else {
return false;
}
}
return false;
}
boolean matching(char ch1, char ch2) {
if ('(' == ch1 && ch2 == ')' || '[' == ch1 && ch2 == ']' || '{' == ch1 && ch2 == '}') {
return true;
}
return false;
}
}
顺便说一下,方法checkParentheses(String str)看起来应该更像这样:
boolean checkParentheses(String str) {
Deque<Character> stack = new ArrayDeque<>();
String open = "({[";
String close = ")]}";
int length = 0;
for (int i = 0; i < str.length(); i++) {
char currentChar = str.charAt(i);
if (open.contains(String.valueOf(currentChar))) {
stack.push(currentChar);
length++;
} else if (close.contains(String.valueOf(currentChar))) {
if (!stack.isEmpty() && matching(stack.peek(), currentChar)) {
stack.pop();
length--;
}
else {
return false;
}
} else {
return false;
}
}
if (length == 0)
return true;
return false;
}
但这完全取决于你......