[LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings

有效括号的嵌套深度。题意是给一个用字符串表示的嵌套括号,请按规则返回这个字符串的嵌套深度depth。嵌套深度的定义如下,

  • depth("") = 0
  • depth(A + B) = max(depth(A), depth(B)), where A and B are VPS‘s
  • depth("(" + A + ")") = 1 + depth(A), where A is a VPS.

例子,

Example 1:

Input: seq = "(()())"
Output: [0,1,1,1,1,0]

Example 2:

Input: seq = "()(())()"
Output: [0,0,0,1,1,0,1,1]

这个题需要用到栈,跟20题类似,需要通过判断遍历的是左括号还是右括号来判断深度。

维护一个栈 s,从左至右遍历括号字符串中的每一个字符:

如果当前字符是 (,就把 ( 压入栈中,此时这个 ( 的嵌套深度为栈的高度;

如果当前字符是 ),此时这个 ) 的嵌套深度为栈的高度,随后再从栈中弹出一个 (。

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/solution/you-xiao-gua-hao-de-qian-tao-shen-du-by-leetcode-s/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int[] maxDepthAfterSplit(String seq) {
 3         if (seq == null || seq.isEmpty()) {
 4             return new int[0];
 5         }
 6         int[] res = new int[seq.length()];
 7         Stack<Integer> stack = new Stack<>();
 8         for (int i = 0; i < seq.length(); i++) {
 9             if (seq.charAt(i) == ‘(‘) {
10                 stack.push(i);
11             } else {
12                 int depth = stack.size();
13                 int left = stack.pop();
14                 if (depth % 2 == 0) {
15                     res[left] = 1;
16                     res[i] = 1;
17                 }
18             }
19         }
20         return res;
21     }
22 }

JavaScript实现

 1 /**
 2  * @param {string} seq
 3  * @return {number[]}
 4  */
 5 var maxDepthAfterSplit = function(seq) {
 6     // corner case
 7     if (seq == null || seq.length == 0) {
 8         return [0];
 9     }
10
11     // normal case
12     let res = new Array(seq.length).fill(0);
13     let stack = [];
14     for (let i = 0; i < seq.length; i++) {
15         let c = seq.charAt(i);
16         if (c == ‘(‘) {
17             stack.push(i);
18         } else {
19             let depth = stack.length;
20             let left = stack.pop();
21             if (depth % 2 == 0) {
22                 res[left] = 1;
23                 res[i] = 1;
24             }
25         }
26     }
27     return res;
28 };

原文地址:https://www.cnblogs.com/aaronliu1991/p/12610065.html

时间: 2024-10-13 15:40:58

[LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings的相关文章

【leetcode】1249. Minimum Remove to Make Valid Parentheses

题目如下: Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. class Solution { public: int maxDepth(TreeNode *root) { inM

Leetcode 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度" 报错如下:   Traceback (most recent call last):   File "<stdin>", line 1, 

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]