1111. The effective depth of nesting brackets

1111. The effective depth of nesting brackets


link

Title Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Thinking

Here Insert Picture Description
Here Insert Picture Description

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;
    }
}

Here Insert Picture Description

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;
    }
Published 55 original articles · won praise 1 · views 852

Guess you like

Origin blog.csdn.net/weixin_42469108/article/details/105255573