-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathValidBrackets.java
More file actions
50 lines (45 loc) · 1.37 KB
/
ValidBrackets.java
File metadata and controls
50 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.haobin.leetcode.stack;
import java.util.Stack;
/**
* @Author HaoBin
* @Create 2020/1/15 16:33
* @Description: 有效的括号
*
* 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
*
* 有效字符串需满足:
* 左括号必须用相同类型的右括号闭合。
* 左括号必须以正确的顺序闭合。
* 注意空字符串可被认为是有效字符串。
*
* 示例 1:
* 输入: "()"
* 输出: true
**/
public class ValidBrackets {
/**
* 利用括号对称的特性,使用栈来保存栈的对应关系
*/
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (stack.empty() || c == '(' || c == '[' || c == '{') {
stack.add(c);
} else if (c == ')' && stack.peek() == '(') {
stack.pop();
} else if (c == ']' && stack.peek() == '[') {
stack.pop();
} else if (c == '}' && stack.peek() == '{') {
stack.pop();
} else {
return false;
}
}
return stack.empty() ? true : false;
}
public static void main(String[] args) {
String s = "(])";
System.out.println(isValid(s));
}
}