官方描述太複雜,其實就是求一個字符串內的有效括號嵌套層級
注意這裏的0和1 不是固定死的,把0變成1在把1變成0也可以。
示例 1:
輸入:seq = "(()())"
輸出:[0,1,1,1,1,0]
示例 2:
輸入:seq = "()(())()"
輸出:[0,0,0,1,1,0,1,1]
class Solution {
public int[] maxDepthAfterSplit(String seq) {
int[] ans = new int [seq.length()];
int idx = 0;
for(char c: seq.toCharArray()) {
ans[idx++] = c == '(' ? (idx + 1) & 1 : idx & 1;
}
return ans;
}
}
代碼來自:Sweetiee
執行用時:1 ms
內存消耗:39.8 MB