如果A子序列的深度小于当前深度的一半,那么也肯定小于最大深度的一半
class Solution { public int[] maxDepthAfterSplit(String seq) { if (seq.length() == 0) return new int[]{}; int[] ans = new int[seq.length()]; // 记录当前深度 int depth = 0; int aDepth = 0; for (int i = 0; i < seq.length(); i++) { if (seq.charAt(i) == '(') { depth++; // 计算当前深度的一半 int mid = 1 + ((depth - 1) / 2); if (aDepth < mid) { ans[i] = 0; aDepth++; } else { ans[i] = 1; } } else { depth--; if (aDepth > 0) { ans[i] = 0; aDepth--; } else { ans[i] = 1; } } } return ans; } }通过奇偶性将深度为奇数的括号与深度为偶数的括号平分
class Solution { public int[] maxDepthAfterSplit(String seq) { if (seq.length() == 0) return new int[]{}; int[] ans = new int[seq.length()]; // 记录当前深度 int depth = 0; for (int i = 0; i < seq.length(); i++) { if (seq.charAt(i) == '(') { ans[i] = ++depth % 2; } else { ans[i] = depth-- % 2; } } return ans; } }