题目描述:
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()" Output: true
Example 2:
Input: "()[]{}" Output: true
Example 3:
Input: "(]" Output: false
Example 4:
Input: "([)]" Output: false
Example 5:
Input: "{[]}" Output: true
代码解答:
package com.jack.algorithm;
import java.util.Stack;
/**
* create by jack 2018/11/1
*
* @author jack
* @date: 2018/11/1 23:01
* @Description:
* 括号匹配
*/
public class ValidParentheses {
/**
* 题目描述:
* https://leetcode.com/problems/valid-parentheses/
* @param s
* @return
*/
public static boolean isValid(String s) {
if (s == null || s.isEmpty()) {
return true;
}
int length = s.length();
Stack<Character> stack = new Stack();
for (int i=0;i<length;i++) {
char c1 = s.charAt(i);
if (c1 == '(' || c1 == '{' || c1 == '[') {
stack.push(c1);
} else {
if (!stack.empty()) {
char c2 = stack.pop();
if (c1 ==')' && c2 == '(') {
continue;
}else if(c1 == '}' && c2 == '{'){
continue;
} else if(c1 == ']' && c2 =='['){
continue;
}else {
return false;
}
} else {
return false;
}
}
}
if (stack.isEmpty()) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
//String s = "()";
//String s = "()[]{}";
//String s = "(]";
//String s = "([)]";
String s = "{[]}";
boolean flag = isValid(s);
System.out.println("flag = "+flag);
}
}
源码地址: