zoukankan      html  css  js  c++  java
  • Add to List 20. Valid Parentheses(LeetCode)

    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.

     1 class Solution {
     2 public:
     3         bool isValid(string s) {
     4         stack<char> st;
     5         int len = s.size();
     6         if (len % 2 != 0)
     7             return false;
     8         for (int i = 0; i < len; i++)
     9         {
    10             if (s[i] == '(' || s[i] == '[' || s[i] == '{')
    11                 st.push(s[i]);
    12             else
    13             {
    14                 if (st.size() == 0)
    15                     return false;
    16                 else
    17                 if (s[i] == m[st.top()])
    18                 {
    19                     //cout << s[i] << m[st.top()] << endl;
    20                     st.pop();
    21                     cout <<st.size()<<st.empty() << endl;
    22                 }
    23                 else
    24                     return false;
    25             }
    26         }
    27         if (st.size() == 0)
    28             return true;
    29         else
    30             return false;
    31     }
    32     map<char, char> m;
    33     Solution()
    34     {
    35         m.insert(pair<char, char>('(', ')'));
    36         m.insert(pair<char, char>('[', ']'));
    37         m.insert(pair<char, char>('{', '}'));
    38     }
    39 };
  • 相关阅读:
    Shader Forge学习
    Unity 编辑器扩展
    DoTween
    游戏AI之群组行为
    Unity与服务区交互数据
    NGUI制作流光效果
    NGUI组件整理总结
    Resharp使用简记
    C#使用LitJson对Json数据解析
    BehaviorDesigner学习
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7124736.html
Copyright © 2011-2022 走看看