栈(4)

来源:互联网 发布:淘宝首页设计怎么设计 编辑:程序博客网 时间:2024/05/19 22:03

原题:

/** * Created by gouthamvidyapradhan on 25/02/2017. * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. * <p> * The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. */

答案:

public class ValidParentheses {    public static void main(String[] args) {        System.out.println(hasBalancedBrackets("(h[e"));    }    private static Map<Character, Character> MAP = new HashMap<>();    // METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED    public static int hasBalancedBrackets(String str) {        if (str == null) return 1;        MAP.put(')', '(');        MAP.put('}', '{');        MAP.put('>', '<');        MAP.put(']', '[');        Stack<Character> stack = new Stack<>();        for (int i = 0, l = str.length(); i < l; i++) {            switch (str.charAt(i)) {                case '(':                case '{':                case '[':                case '<':                    stack.push(str.charAt(i));                    break;                case ')':                case '}':                case ']':                case '>':                    if (stack.isEmpty()) return 0;                    char top = stack.pop();                    if (top != MAP.get(str.charAt(i))) return 0;                    break;                default: //ignore            }        }        return stack.isEmpty() ? 1 : 0;    }}
原创粉丝点击