ValidParentheses

有效的括号

题目介绍

有效的括号

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

1
2
输入: "()"
输出: true

示例 2:

1
2
输入: "()[]{}"
输出: true

示例 3:

1
2
输入: "(]"
输出: false

示例 4:

1
2
输入: "([)]"
输出: false

示例 5:

1
2
输入: "{[]}"
输出: true

题目解法

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
package algorithm;

import java.util.*;

public class ValidParentheses {

public static boolean isValid(String s) {

if (s.length() < 2 || s.length() % 2 == 1) {
return false;
}

Map<Character, Character> map = new HashMap<Character, Character>(){{
put('}', '{');
put(']', '[');
put(')', '(');
}};

char[] chars = s.toCharArray();
Deque<Character> deque = new LinkedList<>();
for (char aChar : chars) {
if (map.get(aChar) != null && deque.size() >= 1 && deque.getLast() == map.get(aChar)) {
deque.pollLast();
} else {
deque.add(aChar);
}
}
return deque.size() <= 0;
}

public static void main(String[] args) {

String s = "()";
System.out.println(isValid(s));

s = "()[]{}";
System.out.println(isValid(s));

s = "(]";
System.out.println(isValid(s));

s = "){";
System.out.println(isValid(s));
}
}

打印:

1
2
3
4
true
true
false
false

思路:

很简单,就是栈。不过java的栈最好不要用stack类,用queue的实现比较好。


ValidParentheses
https://yangtzeshore.github.io/2021/01/24/ValidParentheses/
作者
Chen Peng
发布于
2021年1月24日
许可协议