【LeetCode】 1111.有效括号的嵌套深度(c++暴力版)

原题:

有效括号字符串 定义:对于每个左括号,都能找到与之对应的右括号,反之亦然。详情参见题末「有效括号字符串」部分。

嵌套深度 depth 定义:即有效括号字符串嵌套的层数,depth(A) 表示有效括号字符串 A 的嵌套深度。详情参见题末「嵌套深度」部分。

有效括号字符串类型与对应的嵌套深度计算方法如下图所示:

 

 给你一个「有效括号字符串」 seq,请你将其分成两个不相交的有效括号字符串,A 和 B,并使这两个字符串的深度最小。

不相交:每个 seq[i] 只能分给 A 和 B 二者中的一个,不能既属于 A 也属于 B 。
A 或 B 中的元素在原字符串中可以不连续。
A.length + B.length = seq.length
深度最小:max(depth(A), depth(B)) 的可能取值最小。 
划分方案用一个长度为 seq.length 的答案数组 answer 表示,编码规则如下:

answer[i] = 0,seq[i] 分给 A 。
answer[i] = 1,seq[i] 分给 B 。
如果存在多个满足要求的答案,只需返回其中任意 一个 即可。

示例 1:

输入:seq = "(()())"
输出:[0,1,1,1,1,0]


示例 2:

输入:seq = "()(())()"
输出:[0,0,0,1,1,0,1,1]
解释:本示例答案不唯一。
按此输出 A = "()()", B = "()()", max(depth(A), depth(B)) = 1,它们的深度最小。
像 [1,1,1,0,0,1,1,1],也是正确结果,其中 A = "()()()", B = "()", max(depth(A), depth(B)) = 1 。
 

提示:

1 <= text.size <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

主要思路是遍历字符串,遇'('计数加一,遇‘)’计数减一,当计数变为最小的最大深度或零时,如果下一次变化或导致计数不再在[0, maxDepth]中,就改变下一个字符的分组。

 1 #include<iostream>
 2 #include<vector>
 3 #include<math.h>
 4 #include<string>
 5 
 6 using namespace std;
 7 
 8 vector<int> maxDepthAfterSplit(string seq);
 9 int switchStatus(int a);
10 int countDepth(string seq);
11 
12 int main() {
13     string seq;
14     getline(cin, seq);
15     vector<int> result = maxDepthAfterSplit(seq);
16     cout << '[';
17     for (int i = 0; i < result.size(); ++i) {
18         if (i < result.size() - 1) {
19             cout << result[i] << ',';
20         }
21         else {
22             cout << result[i] << ']' << endl;
23         }
24     }
25     system("pause");
26 }
27 
28 vector<int> maxDepthAfterSplit(string seq) {
29     int depth = countDepth(seq);
30     int maxDepth = ceil(depth / 2); //最小的最大深度即原嵌套深度除二后向上取整
31     int switchABFlag = 0; //改变分组
32     int count = 0; //计数
33     vector<int> splitList;
34 
35     for (int i = 0; i < seq.length(); i++) {
36         if (seq[i] == '(') {
37             count++;
38             splitList.push_back(switchABFlag);
39                         //如果count即将超过maxDepth,改变分组
40             if (count == maxDepth && seq[i + 1] == '(') {
41                 count = 0;
42                 switchABFlag = switchStatus(switchABFlag);
43             }
44         }
45         else if(seq[i] == ')') {
46             count--;
47             splitList.push_back(switchABFlag);
48                         //如果count即将小于0,改变分组
49             if (count == 0 && seq[i + 1] == ')') {
50                 count = maxDepth;
51                 switchABFlag = switchStatus(switchABFlag);
52             }
53         }
54     }
55     return splitList;
56 }
57 
58 //改变分组
59 int switchStatus(int a) {
60     if (a == 0) 
61         return 1;
62     else 
63         return 0;
64 }
65 
66 //求原字符串的嵌套层数
67 int countDepth(const string seq) {
68     int len = seq.length();
69     int max = 0;
70     int count = 0;
71     for (int i = 0; i < len; ++i) {
72         if (seq[i] == '(') {
73             ++count;
74             if (max < count) {
75                 max = count;
76             }
77         }
78         else {
79             --count;
80         }
81     }
82     return max;
83 }

暴力解完后看了下官方的解答,方才知道自己是个鶸≡(▔﹏▔)≡

 1 class Solution {
 2 public:
 3     vector<int> maxDepthAfterSplit(string seq) {
 4         int d = 0;
 5         vector<int> ans;
 6         for (char& c : seq)
 7             if (c == '(') {
 8                 ++d;
 9                 ans.push_back(d % 2);
10             }
11             else {
12                 ans.push_back(d % 2);
13                 --d;
14             }
15         return ans;
16     }
17 };
18 
19 作者:LeetCode-Solution
20 链接: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/
21 来源:力扣(LeetCode)
22 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/Neptunejiang/p/12615339.html