Leetcode 32. 最长有效括号(DAY 102) ---- Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首刷自解)


class Solution {
public:
    int longestValidParentheses(string s) {
        stack<int> intstack,tempstack;
        int ret = 0,temp = 0,charstack = 0;

        for(const auto& chr:s)
        {
            if(chr == '(')
            {
                intstack.emplace(-1);
                ++charstack;
            }
            else
            {
                if(!charstack)
                {
                    intstack = tempstack;
                    continue;
                }   
                else
                {
                    --charstack;
                    auto temp = 2;
                    while(!intstack.empty() && intstack.top() != -1)
                    {
                        temp += intstack.top();
                        intstack.pop();
                    }
                    intstack.pop();
                    while(!intstack.empty() && intstack.top() != -1)
                    {
                        temp += intstack.top();
                        intstack.pop();
                    }
                    ret = max(temp,ret);
                    intstack.emplace(temp);
                }
            }
        }

        return ret;
    }
};

代码实现(二刷自解 DAY 286 C++ 空间复杂度稍高)


class Solution {
 public:
  int longestValidParentheses(string s) {
    stack<char> chrstack;
    stack<int> intstack;
    int ret = 0;

    for (const auto chr : s) {
      if (chr == ')') {
        if (chrstack.empty() || chrstack.top() == ')') {
          {
            stack<char> tmpchr;
            stack<int> tmpint;
            chrstack.swap(tmpchr);
            intstack.swap(tmpint);
          }
          continue;
        }
        chrstack.pop();
        int tmp = intstack.top();        
        intstack.pop();
        if (tmp != 0) intstack.pop();
        tmp += 2;

        while (!intstack.empty() && intstack.top() != 0) {
          intstack.top() += tmp;
          tmp = intstack.top(); 
          intstack.pop();
        }
        intstack.emplace(tmp);
        ret = max(ret, intstack.top());
      } else {
        intstack.emplace(0);
        chrstack.emplace(chr);
      }
    }
    return ret;
  }
};

代码实现(三刷自解 DAY 286 C++)


class Solution {
 public:
  int longestValidParentheses(string s) {
    stack<int> intstack;
    int ret = 0, chrstack = 0;

    for (const auto chr : s) {
      if (chr == '(') {
        intstack.emplace(0);
        ++chrstack;
      } else {
        if (chrstack == 0) {
          stack<int> tmp;
          intstack.swap(tmp);
          continue;
        }
        --chrstack;
        int tmp = intstack.top() + 2;
        intstack.pop();
        if (tmp != 2) {
          intstack.pop();
        }

        while (!intstack.empty() && intstack.top() != 0) {
          tmp = intstack.top() + tmp;
          intstack.pop();
        }
        intstack.emplace(tmp);
        ret = max(ret, tmp);
      }
    }
    return ret;
  }
};

代码实现(四刷自解 DAY 7 Golang)


func max(x, y int) int {
  if x >= y {
    return x
  } else {
    return y
  }
}

func longestValidParentheses(s string) int {
  chrstack, intstack := []byte{}, []int{}
  ret := 0

  for _, chr := range s {
    if chr == '(' {
      chrstack = append(chrstack, '(')
      intstack = append(intstack, 0)
    } else {
      if len(chrstack) == 0 {
        intstack = []int{}
        continue
      }

      topnum := intstack[len(intstack) - 1]
      intstack = intstack[:len(intstack) - 1]
      if topnum != 0 {
        topnum += intstack[len(intstack) - 1]
        intstack = intstack[:len(intstack) - 1]
      }
      topnum += 2
      chrstack = chrstack[:len(chrstack) - 1]

      for len(intstack) != 0 && intstack[len(intstack) - 1] != 0 {
        topnum += intstack[len(intstack) - 1]
        intstack = intstack[:len(intstack) - 1]
      }
      intstack = append(intstack, topnum)
      ret = max(ret, topnum)
    }
  }
  return ret
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值