1111. The effective depth of nesting brackets
link
Title Description
Thinking
class Solution {
public int[] maxDepthAfterSplit(String seq) {
if(seq == null){
return new int[0];
}
int idx = 0;
int[] res = new int[seq.length()];
for(char c : seq.toCharArray()){
res[idx++] = c == '(' ? (idx&1) : ((idx+1)&1);
}
return res;
}
}
public int[] maxDepthAfterSplit(String seq) {
int[] res = new int[seq.length()];
int index = 0;
for(char c : seq.toCharArray())
{
//如果当前字符是左括号
if(c == '(')
{
//如果index++后是奇数,index在数组中的对应位置是0,否则是1
res[index++] = (index + 1) & 1 ;
}
//如果当前字符是右括号
else{
//如果index++后是奇数,index在数组中的对应位置是1,否则是0
res[index++] = index & 1 ;
}
}
return res;
}