zoukankan      html  css  js  c++  java
  • Leetcode 20

    //嘻嘻,垃圾的我写了一个小时,各种bug,丑的要死
    class
    Solution { public: bool isValid(string s) { stack<char> st; if(s == "") return true; if(s[0] == ')' || s[0] == ']' || s[0] == '}') return false; st.push(s[0]); int cnt = 1; while(1){ if(st.top() == '('){ if(s[cnt] == ')'){st.pop();cnt++;} else if(s[cnt] == '['||s[cnt] == '{'||s[cnt] == '('){st.push(s[cnt++]);} else return false; } else if(st.top() == '['){ if(s[cnt] == ']'){st.pop();cnt++;} else if(s[cnt] == '['||s[cnt] == '{'||s[cnt] == '('){st.push(s[cnt++]);} else return false; } else if(st.top() == '{'){ if(s[cnt] == '}'){st.pop();cnt++;} else if(s[cnt] == '['||s[cnt] == '{'||s[cnt] == '('){st.push(s[cnt++]);} else return false; } else return false; if(cnt < s.size()&&st.empty()){st.push(s[cnt++]);} if(cnt >= s.size()) break; } if(st.empty()) return true; else return false; } };

     别人写的:差距啊!

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> parentheses;
            for (int i = 0; i < s.size(); ++i) {
                if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]);
                else {
                    if (parentheses.empty()) return false;
                    if (s[i] == ')' && parentheses.top() != '(') return false;
                    if (s[i] == ']' && parentheses.top() != '[') return false;
                    if (s[i] == '}' && parentheses.top() != '{') return false;
                    parentheses.pop();
                }
            }
            return parentheses.empty();
        }
    };
  • 相关阅读:
    谈谈jQuery之绑定事件
    手机移动端WEB资源整合
    2016移动端web5分钟速成(适合新手)
    日常整理的一些网址
    web服务器页面错误代码集
    jQuery事件绑定的最佳实践
    前端体系
    js刷新页面的几种方法
    JAVA的静态变量、静态方法、静态类
    在Hadoop集群中添加机器和删除机器
  • 原文地址:https://www.cnblogs.com/cunyusup/p/9629069.html
Copyright © 2011-2022 走看看