标签:++ 示例 problems max val 链接 tps spl etc
/*示例 1:
输入:seq = "(()())"
输出:[0,1,1,1,1,0]
示例 2:
输入:seq = "()(())()"
输出:[0,0,0,1,1,0,1,1]
链接:https://leetcode-cn.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings*/
分为两个部分,使得两部分深度差值最小(两个深度最大值最小)
//直观解法
public int[] maxDepthAfterSplit2(String seq) {
int[] ans = new int[seq.length()];
char[] chars = seq.toCharArray();
int i = 0, j = 0, k = 0;
for (char c:chars) {
if(c==‘(‘){
if(j>k){
ans[i] = 1;
k++;
}else{
ans[i] = 0;
j++;
}
}else{
if(j>=k){
ans[i] = 0;
j--;
}else{
ans[i] = 1;
k--;
}
}
i++;
}
return ans;
}
//通过奇偶性判断
public int[] maxDepthAfterSplit(String seq) {
int[] ans = new int[seq.length()];
char[] chars = seq.toCharArray();
int i = 0;
for (char c:chars) {
if(c==‘(‘){//‘(’从0开始,偶数下标为0,奇数下标为1
ans[i]=i%2;
}else{//‘)‘与‘(‘相反
ans[i]=1-i%2;
}
i++;
}
return ans;
}
标签:++ 示例 problems max val 链接 tps spl etc
原文地址:https://www.cnblogs.com/CoderRdf/p/12613305.html