[Inscription] effective brackets nesting depth -leetcode

Title: Effective parentheses nesting depth

String Defines a valid brackets: for each of a left parenthesis, right parenthesis corresponding thereto can be found, and vice versa. For details, see "Effective brackets string" section of the end of the title.

Nesting depth defined depth: i.e. the effective string parentheses nesting depth, depth (A) represents the effective depth of the nesting A string of brackets. For details, see the end of the title "nesting depth" section.

 

To give you a "valid parenthesis string" seq, you divide it into two disjoint brackets valid strings, A and B, and the minimum depth of the two strings.

Disjoint: Each seq [i] can only be given to both A and B a, A can not both belong to B belongs.
A B the elements in the original string or may not be continuous.
+ = Seq.length B.length a.length
max (depth (A), depth (B)) is the smallest possible values.
Partitioning scheme with a length of seq.length answer answer array, said encoding rules are as follows:

answer [i] = 0, seq [i] points to A.
answer [i] = 1, seq [i] points to B.
If there is more than meet the requirements of the answer, simply return any of which can be.

 

Example 1:

Input: seq = "(() ( ))"
Output: [0,1,1,1,1,0]
Example 2:

Input: seq = "() (( )) ()"
Output: [0,0,0,1,1,0,1,1]
 

prompt:

1 <= text.size <= 10000
 

Effective brackets string:

Only by "(" and ")" character string composed of, for each of the left parenthesis, right parenthesis corresponding thereto can be found, and vice versa.
The following situations equally belongs to a valid string brackets:

1. empty string
2 is connected, can be written as AB (A is connected to B), wherein A and B are valid string brackets
3. nesting, can be written as (A), where A is a valid string brackets
fitted sets depth:

Similarly, we can define the effective depth of nesting brackets string s depth (S):

1. s is empty, depth ( "") = 0
when B 2. s is connected to the A, depth (A + B) = max (depth (A), depth (B)), where A and B are effective brackets string
3. s nested case, depth ( "(" + A + ")") = 1 + depth (A), where A is a valid string brackets

For example: "", "() ()" and "() (() ())" is a valid character string in brackets, 0,1,2 nesting depth respectively, while ") (" and "(( ) "is not a valid string brackets.


This topic is really hard to understand. . .

Title probably talking about is:

  These brackets string into two pile legitimate sequence, let the smallest nesting.

  In fact, very simple, with a stack to match the string, and then the even and odd string to string into two camps.

On the code:

c:

int* maxDepthAfterSplit(char * seq, int* returnSize){
    int len = strlen(seq),ans = 0;
    *returnSize = len;
    
    int *p = ( int *)malloc( sizeof( int )*len );
    
    for ( int i = 0; i < len; i++ ) {
        if( seq[i] == '(' ) p[i] = (++ans)%2;
        if( seq[i] == ')' ) p[i] = (ans--)%2; 
    }
    return p;
}

 

c++:

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

2020-04-0114:42:47

Guess you like

Origin www.cnblogs.com/Sxccz/p/leetcode_12.html