zoukankan      html  css  js  c++  java
  • Valid Parentheses

    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.

    Subscribe to see which companies asked this question

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> symbol_container;
    
            int len = s.length();
            int i;
            for (i=0; i<len; i++){
                if (check_open_symbol(s[i])) {
                    symbol_container.push(s[i]);
                } else if (check_close_symbol(s[i])) {
                    if (!symbol_container.empty() && (symbol_container.top() == get_match_symbol(s[i]))) {
                        symbol_container.pop();
                    } else {
                        return false;
                    }
                }
            }
            if (symbol_container.empty()) {
                return true;
            } else {
                return false;
            }
    
        }
    
        bool check_open_symbol(char c) {
            if (c =='(' || c == '[' || c == '{') {
                return true;
            }
            return false;
        }
    
        bool check_close_symbol(char c) {
            if ( c == '}' || c == ')' || c == ']') {
                return true;
            }
            return false;
        }
    
        char get_match_symbol(char c) {
            if (c == '{'){
                return '}';
            } else if (c == '}') {
                return '{';
            }else if (c == '(') {
                return ')';
            }else if (c == ')') {
                return '(';
            }else if (c == ']') {
                return '[';
            }else if (c == '[') {
                return ']';
            }
        }
    };
  • 相关阅读:
    1004: 画图
    1002: 数字排序问题
    1003: 相邻数对问题
    1001: 图像旋转问题
    1000: 数塔
    springday05-go1
    springday04-go2
    springday04-go1
    springday03-go2
    Android—PopupWindow的简单使用
  • 原文地址:https://www.cnblogs.com/SpeakSoftlyLove/p/5111103.html
Copyright © 2011-2022 走看看