zoukankan      html  css  js  c++  java
  • 20. Valid Parentheses

    description:

    check whether the (){}[] is valid(is pair)
    Note:

    Example:

    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
    

    my answer:

    感恩

    my answer

    一眼想到堆栈,奈何堆栈没有想起我,堆栈的用法get一下ok吧...
    

    大佬的answer:

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> res;
            for(int i = 0; i < s.size(); ++i){
                if(s[i] == '(' || s[i] == '[' || s[i] == '{') res.push(s[i]);
                else{
                    if(res.empty()) return false;
                    if(s[i] == ')' && res.top() != '(')return false;
                    if(s[i] == ']' && res.top() != '[')return false;
                    if(s[i] == '}' && res.top() != '{')return false;
                    res.pop();
                }
            }
            return res.empty();
        }
    };
    

    relative point get√:

    stackres; //definite a stack
    stack.top(); // the element on the top of the stack
    stack.push(xxx); // push a element on the top of the stack
    stack.pop(); // pop the element on the top of the stack
    stack.empty();

    hint :

  • 相关阅读:
    c# 面相对象4-多态性
    c# 面相对象3-之继承性
    c# 面相对象2-之封装性
    面向对象和面向过程的区别
    <title>下拉菜单</title>
    15-07-31 javascript--事件
    DOM操作
    格式与布局
    c# 函数相关练习
    c# 哈希表集合;函数
  • 原文地址:https://www.cnblogs.com/forPrometheus-jun/p/10683456.html
Copyright © 2011-2022 走看看