1. Topic
Bracket only by valid string "("
and ")"
configured, and in accordance with one of several of the following conditions:
- Null string
- Connection may be referred to as AB (A and B are connected), wherein A and B are valid string brackets
- Nesting, can be written as (A), where A is a valid string brackets
Similarly, we can define the effective depth of nesting brackets string s depth (S):
- s is empty,
depth("") = 0
- When s is connected to A and B,
depth(A + B) = max(depth(A), depth(B))
where A and B are valid string brackets - s is nested case
depth("(" + A + ")") = 1 + depth(A)
where A is a valid string brackets
example:"","()()",和 "()(()())"
are valid string brackets, respectively, 0,1,2 nesting depth, and")(" 和 "(()"
is not effective in parentheses string.
To give you a valid string bracket seq, dividing it into two disjoint sub-sequences A and B, and A and B satisfy the brackets define a valid character string (note: A.length + B.length = seq.length).
Now, you need to choose the arbitrary set of valid strings brackets A and B, make max(depth(A), depth(B))
possible the value of the minimum .
Returns the length of seq.length
the answer array answer
, select A or B encoding rule is: if seq [i] is the A part, then the answer [i] = 0. Otherwise, answer [i] = 1.
Even if there are multiple answers exist to meet the requirements, you also need a return.
示例 1:
输入:seq = "(()())"
输出:[0,1,1,1,1,0]
示例 2:
输入:seq = "()(())()"
输出:[0,0,0,1,1,0,1,1]
提示:
1 <= text.size <= 10000
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
2. Problem Solving
- Title mean, as much as possible so that a minimum of two open brackets maximum nesting depth
- Then according to the depth into odd layers, even layers, respectively, out on the line
class Solution {
public:
vector<int> maxDepthAfterSplit(string seq) {
int i, j = 0, depth = 0;
vector<int> ans(seq.size(),0);
char prev = '*';
for(i = 0; i < seq.size(); ++i,++j)
{
if(prev == ')')
depth--;
if(seq[i] == '(')
depth++;
if(depth & 1)//奇数层
ans[j] = 1;
prev = seq[i];
// cout << depth << " ";
}
return ans;
}
};