zoukankan      html  css  js  c++  java
  • Leetcode刷题记录---20. Valid Parentheses

    菜鸡我决定提升一下自己代码能力,为了不打击自己的自信心,这次就先从简单的开始了

    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.

    class Solution {
    public:
         bool isValid(string s) {
            stack<char> stack;
            char c;
            int i = 0;
           
            while(s[i] != ''){
                c= s[i];
                //cout << "c=" << c << endl;
                if(c == '(' || c == '{' || c == '['){
                    stack.push(c);
                }else if(!stack.empty() && (( c == ')' && stack.top() == '(') || (c == '}' && stack.top() == '{') || (c == ']' && stack.top() == '[') )){
                    stack.pop();
                
                }else {
                    stack.push(c);
                }
              i++;
            }
    
            if(stack.empty()){
                return true;
            }else{
                return false;
            }
        }
    };
    


    幸好结果还行,我的心脏还能承受得起哈哈

  • 相关阅读:
    工科物理实验()中国大学MOOC答案(已更新)
    类似jar文件使用java无法打开问题
    python9、10章
    nmap的理解与利用(初级)
    常见端口
    配置优化
    删除表操作
    万能的map
    测试
    Mapper.xml
  • 原文地址:https://www.cnblogs.com/yuyuan-bb/p/12604358.html
Copyright © 2011-2022 走看看