美文网首页
括号问题

括号问题

作者: juexin | 来源:发表于2017-04-18 15:46 被阅读0次

**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);
    }
};

相关文章

  • 括号问题

    **20. Valid Parentheses **Given a string containing just ...

  • 3. 一些算法问题

    1. 括号匹配问题 算法:括号匹配问题 - 简书 C程序括号匹配检查 - Jason ZHANG的博客 - CSD...

  • 括号匹配问题

    这个问题的描述很简单,就是若括号匹配则返回true,否则返回false。 Given a string conta...

  • 括号匹配问题

    问题描述 有效字符串需满足: 左括号必须用相同类型的右括号闭合。包括:“( )”,“[ ]”,“{ }”。左括号必...

  • 括号类问题

    参考文章:https://labuladong.gitee.io/algo/di-san-zha-24031/ji...

  • 认认真真刷Leetcode:Valid Parentheses

    Valid Parentheses (有效的括号问题) 问题难度:easy 题目描述: 细节: 1:开放的括号必须...

  • 利用堆栈解决的问题

    有效括号问题 题目 解法 遇到左括号时push,遇到右括号pop。 等待时间问题 题目 解法 将天数指针存入堆栈中...

  • chap3-栈和队列

    括号匹配问题 // 括号匹配,遇到 '\0' 结束// 遇到花、中、圆左括号进栈,遇到花、中、圆右括号检查栈顶元素...

  • 括号匹配等一系列问题

    括号匹配等一系列问题 问题描述 左括号右边能找到唯一对应的右括号,右括号左边能找到唯一对应的左括号,则该字符串为完...

  • JavaScript 平衡括号算法

    何谓平衡括号问题?平衡括号也叫有效括号,问题描述有很多种,看下面中的一种描述: 给定一个只包括 '(',')','...

网友评论

      本文标题:括号问题

      本文链接:https://www.haomeiwen.com/subject/pwotzttx.html