在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A string is a valid parentheses string (denoted VPS) if and only if it consists of
We can similarly define the nesting depth
For example, Given a VPS seq, split it into two disjoint subsequences Now choose any such Return an Example 1: Input: seq = "(()())" Output: [0,1,1,1,1,0] Example 2: Input: seq = "()(())()" Output: [0,0,0,1,1,0,1,1] Constraints:
有效括号字符串 仅由
类似地,我们可以定义任意有效括号字符串
例如: 给你一个有效括号字符串 现在,你需要从中选出 任意 一组有效括号字符串 返回长度为 示例 1: 输入:seq = "(()())" 输出:[0,1,1,1,1,0] 示例 2: 输入:seq = "()(())()" 输出:[0,0,0,1,1,0,1,1] 提示:
24ms
1 class Solution { 2 func maxDepthAfterSplit(_ seq: String) -> [Int] { 3 let N = seq.count 4 var chars = Array(seq) 5 var result = [Int](repeating:0, count: N) 6 for i in 0..<N { 7 result[i] = (chars[i] == "(") ? (i & 1) : (1 - i & 1) 8 } 9 return result 10 } 11 } Runtime: 32 ms Memory Usage: 21.4 MB
1 class Solution { 2 func maxDepthAfterSplit(_ seq: String) -> [Int] { 3 let n:Int = seq.count 4 var levels:[Int] = [Int](repeating:0,count:n + 1) 5 let arrSeq:[Character] = Array(seq) 6 for i in 0..<n 7 { 8 let num:Int = arrSeq[i] == "(" ? +1 : -1 9 levels[i + 1] = levels[i] + num 10 } 11 let max_level:Int = levels.max()! 12 let half:Int = max_level / 2 13 var answer:[Int] = [Int](repeating:0,count:n) 14 for i in 0..<n 15 { 16 answer[i] = min(levels[i], levels[i + 1]) < half ? 1 : 0 17 } 18 return answer 19 } 20 } 32ms 1 class Solution { 2 func maxDepthAfterSplit(_ seq: String) -> [Int] { 3 var arr = [Int]() 4 for i in 0..<seq.length { 5 arr.append(1) 6 } 7 return arr 8 } 9 }
|
请发表评论