zoukankan      html  css  js  c++  java
  • [LeetCode#20]Valid Parentheses

    The quesiton:

    Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

    The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

    My analysis:

    Pair brackets is a very common usage of stack.
    The basic idea is:
    1. iff the bracket is a left bracket, we push it onto the stack.
    2. iff the bracket is a right bracket, we pop a bracket from the stack, and compare it with the bracket.
    Corner cases:
    1. "]"
    If we pop the stack directly, it would result in an exception.
    We should firstly test if the stack is empty, iff empty, the string must be invalid!

    2. "[[]"
    This case represent the situation, when all right brackets was paired, there are some left brackets remained in the stack.
    This case could be detected by test if the final stack is empty. iff not, the string is invalid!

    My solution:

    public class Solution {
        public boolean isValid(String s) {
            Stack<Character> l_stack = new Stack<Character> ();//must box the primitive type before use stack
            char temp;
            
            for (int i = 0; i < s.length(); i++) {
                    if (is_left_bracket(s.charAt(i))) {
                        l_stack.push(s.charAt(i));
                    } else {
                        if (l_stack.isEmpty()) // if the stack is empty, return false
                            return false;
                            
                        temp = l_stack.pop(); //try to pop an element from a stack would result in exception!
                        if (! is_brackets_match(temp, s.charAt(i))) //check if the two brackts matched!
                            return false; 
                    }
            }
            
            if (l_stack.isEmpty()) //test if all left brackets has already been paired 
                return true;
            else 
                return false;
        }
        
        static public boolean is_brackets_match(char b1, char b2) {
            
            if (b1 == '(' && b2 == ')')
                return true; 
            if (b1 == '{' && b2 == '}')
                return true;
            if (b1 == '[' && b2 == ']')
                return true; 
            
            return false;
        }
        
        static public boolean is_left_bracket(char bracket) {
            switch (bracket) {
                case '(' : 
                    return true;
                case '{' : 
                    return true;
                case '[' : 
                    return true;
                default : 
                    return false;
            }
        }
    }
  • 相关阅读:
    从零入门 Serverless | Serverless Kubernetes 应用部署及扩缩容
    从单体迈向 Serverless 的避坑指南
    从零入门 Serverless | 教你使用 IDE/Maven 快速部署 Serverless 应用
    开发函数计算的正确姿势——OCR 服务
    从零入门 Serverless | 函数计算的开发与配置
    全部满分!阿里云函数计算通过可信云21项测试
    登录接口+三方登录 微博
    注册接口文档
    异步发送短信验证与 注册接口完善
    图片验证码接口
  • 原文地址:https://www.cnblogs.com/airwindow/p/4193089.html
Copyright © 2011-2022 走看看