LeetCode 1111 - effective parentheses nesting depth

Title Description

1111. The effective depth of nesting brackets

Solution one: Stack simulation

Reference nesting depth effective parentheses

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq) {
        int d = 0;
        vector<int> ans;
        for(auto c: seq)
        {
            if(c=='(')
            {
                d++;
                ans.push_back(d%2);
            }
            else
            {
                ans.push_back(d%2);
                d--;
            }
        }
        return ans;
    }
};

Solution two: find the law

Reference nesting depth effective parentheses

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq) {
        vector<int> ans;
        for(int i=0;i<(int)seq.size();i++)
        {
            ans.push_back(i&1^(seq[i]=='('));
        }
        return ans;
    }
};
Published 152 original articles · won praise 22 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_38204302/article/details/105262207