1.题目描述:
给定一个可能包括( ) [ ] { }的字符串,判断字符串是否有效(有效返回true,无效返回false)
有效字符串需要满足:
- · 左括号必须用相同类型的右括号闭合
- · 左括号必须以正确的顺序闭合
- · 注意空字符串可被认为是有效字符串
1.1 代码1
import edu.princeton.cs.algs4.Stack;
public class xiaPi {
public static void main(String[] args) {
boolean res = checkKuoHao("{1[2(3)]}");
System.out.println("{1[2(3)]}" + "==>" + res);
res = checkKuoHao("123");
System.out.println("123" + "==>" + res);
res = checkKuoHao("(");
System.out.println("(" + "==>" + res);
res = checkKuoHao("([])");
System.out.println("([])" + "==>" + res);
res = checkKuoHao("(]");
System.out.println("(]" + "==>" + res);
}
public static boolean checkKuoHao(String str) {
if (str.length() == 0) return true;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
Character c = str.charAt(i);
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
}
if (c == ')') {
if (stack.peek() == '(') {
stack.pop();
}
else {
return false;
}
}
if (c == ']') {
if (stack.peek() == '[') {
stack.pop();
}
else {
return false;
}
}
if (c == '}') {
if (stack.peek() == '{') {
stack.pop();
}
else {
return false;
}
}
}
if (stack.size() == 0) {
return true;
}
else {
return false;
}
}
}
1.2 代码2,修正后的代码
import edu.princeton.cs.algs4.Stack;
public class xiaPi {
public static void main(String[] args) {
// boolean res = checkKuoHao("{1[2(3)]}");
boolean res = checkKuoHao("123");
System.out.println(res);
}
public static boolean checkKuoHao(String str) {
if (str.length() == 0) return true;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
Character c = str.charAt(i);
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
}
if (c == ')' && stack.peek() == '(') {
stack.pop();
}
if (c == ']' && stack.peek() == '[') {
stack.pop();
}
if (c == '}' && stack.peek() == '{') {
stack.pop();
}
}
System.out.println(stack.size());
System.out.println(stack.toString());
if (stack.size() == 0) {
return true;
}
else {
return false;
}
}
}
2.测试用例
2.1测试结果
{1[2(3)]}>true
123>true
(>false
([])>true
(]==>false
3.源代码
/* *****************************************************************************
* 给定一个可能包括( ) [ ] { }的字符串,判断字符串是否有效(有效返回true,无效返回false)
* 有效字符串需要满足:
* · 左括号必须用相同类型的右括号闭合
* · 左括号必须以正确的顺序闭合
* · 注意空字符串可被认为是有效字符串
**************************************************************************** */
import edu.princeton.cs.algs4.Stack;
public class xiaPi {
public static void main(String[] args) {
// boolean res = checkKuoHao("{1[2(3)]}");
boolean res = checkKuoHao("123");
System.out.println(res);
}
public static boolean checkKuoHao(String str) {
if (str.length() == 0) return true;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
Character c = str.charAt(i);
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
}
if (c == ')' && stack.peek() == '(') {
stack.pop();
}
if (c == ']' && stack.peek() == '[') {
stack.pop();
}
if (c == '}' && stack.peek() == '{') {
stack.pop();
}
}
System.out.println(stack.size());
System.out.println(stack.toString());
if (stack.size() == 0) {
return true;
}
else {
return false;
}
}
}