我的一种思路是递归:
private static String s = "(2344[)]53[234]{2324}"; private static boolean f(int x, int y) { for (int i = x; i < s.length(); i++) { if (s.charAt(i) == '(') { return f(i + 1, 1); } else if (s.charAt(i) == '[') { return f(i + 1, 2); } else if (s.charAt(i) == '{') { return f(i + 1, 3); } else if (s.charAt(i) == ')') { return y == 1; } else if (s.charAt(i) == ']') { return y == 2; } else if (s.charAt(i) == '}') { return y == 3; } } return true; } public static void main(String[] args) { System.out.println(f(0,0)); }