括号问题

**20. 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.

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

**32. Longest Valid Parentheses **
跟上题思路相同
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
代码如下:

class Solution {
public:
    int longestValidParentheses(string s) {
        if(s.size()<=1)
          return 0;
        stack<int> temp;
        int last = -1;
        int maxLen = 0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='(')
              temp.push(i);
            else
            {
                if(temp.empty())
                  last = i;
                else
                {
                    //int t = temp.top();
                    temp.pop();
                    if(temp.empty())
                      maxLen = max(maxLen,i-last);
                    else
                      maxLen = max(maxLen,i-temp.top());
                }
            }
        }
        return maxLen;
    }
};

**22. Generate Parentheses **
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

代码如下:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> rec;
        dfs(n,n,"",rec);
        return rec;
    }
    void dfs(int left,int right,string out,vector<string> &rec)
    {
        if(left>right)
          return;
        if(left==0&&right==0)
          rec.push_back(out);
        if(left>0)
          dfs(left-1,right,out + "(",rec);
        if(right>0)
          dfs(left,right-1,out + ")",rec);
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,774评论 0 33
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,906评论 0 38
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,936评论 0 0
  • 子曰:“由!诲女知之乎!知之为知之,不知为不知,是知也。” 知之为知之,不知为不知:知道就是知道,不知道就是不知道...
    白痴老猫阅读 727评论 2 1
  • 我是142号星宝宝梁善惠,正在参加日记星球第5期21天蜕变之旅的复训。 同时也是小牛妈妈英语启蒙A42号学员,父母...
    梁姑娘悦阅读 153评论 0 1