File tree 6 files changed +292
-3
lines changed
0700-0799/0761.Special Binary String
1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings
6 files changed +292
-3
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ def makeLargestSpecial(self, s: str) -> str:
8
8
while i < len (s ):
9
9
cnt += 1 if s [i ] == '1' else - 1
10
10
if cnt == 0 :
11
- ans .append ('1' + self .makeLargestSpecial (s [j + 1 : i ]) + '0' )
11
+ ans .append ('1' + self .makeLargestSpecial (s [j + 1 : i ]) + '0' )
12
12
j = i + 1
13
13
i += 1
14
14
ans .sort (reverse = True )
Original file line number Diff line number Diff line change 93
93
<!-- 这里可写当前语言的特殊实现逻辑 -->
94
94
95
95
``` python
96
-
96
+ class Solution :
97
+ def maxDepthAfterSplit (self , seq : str ) -> List[int ]:
98
+ ans = [0 ] * len (seq)
99
+ a = b = 0
100
+ for i, c in enumerate (seq):
101
+ if c == " (" :
102
+ if a < b:
103
+ a += 1
104
+ else :
105
+ b += 1
106
+ ans[i] = 1
107
+ else :
108
+ if a > b:
109
+ a -= 1
110
+ else :
111
+ b -= 1
112
+ ans[i] = 1
113
+ return ans
97
114
```
98
115
99
116
### ** Java**
100
117
101
118
<!-- 这里可写当前语言的特殊实现逻辑 -->
102
119
103
120
``` java
121
+ class Solution {
122
+ public int [] maxDepthAfterSplit (String seq ) {
123
+ int [] res = new int [seq. length()];
124
+ for (int i = 0 , cnt = 0 ; i < res. length; ++ i) {
125
+ if (seq. charAt(i) == ' (' ) {
126
+ res[i] = cnt++ & 1 ;
127
+ } else {
128
+ res[i] = -- cnt & 1 ;
129
+ }
130
+ }
131
+ return res;
132
+ }
133
+ }
134
+ ```
135
+
136
+ ``` java
137
+ class Solution {
138
+ public int [] maxDepthAfterSplit (String seq ) {
139
+ int n = seq. length();
140
+ int [] ans = new int [n];
141
+ int a = 0 , b = 0 ;
142
+ for (int i = 0 ; i < n; ++ i) {
143
+ char c = seq. charAt(i);
144
+ if (c == ' (' ) {
145
+ if (a < b) {
146
+ ++ a;
147
+ } else {
148
+ ++ b;
149
+ ans[i] = 1 ;
150
+ }
151
+ } else {
152
+ if (a > b) {
153
+ -- a;
154
+ } else {
155
+ -- b;
156
+ ans[i] = 1 ;
157
+ }
158
+ }
159
+ }
160
+ return ans;
161
+ }
162
+ }
163
+ ```
164
+
165
+ ### ** C++**
166
+
167
+ ``` cpp
168
+ class Solution {
169
+ public:
170
+ vector<int > maxDepthAfterSplit(string seq) {
171
+ int n = seq.size();
172
+ vector<int > ans(n);
173
+ int a = 0, b = 0;
174
+ for (int i = 0; i < n; ++i)
175
+ {
176
+ char c = seq[ i] ;
177
+ if (c == '(')
178
+ {
179
+ if (a < b) ++a;
180
+ else ++b, ans[ i] = 1;
181
+ }
182
+ else
183
+ {
184
+ if (a > b) --a;
185
+ else --b, ans[ i] = 1;
186
+ }
187
+ }
188
+ return ans;
189
+ }
190
+ };
191
+ ```
104
192
193
+ ### **Go**
194
+
195
+ ```go
196
+ func maxDepthAfterSplit(seq string) []int {
197
+ ans := make([]int, len(seq))
198
+ a, b := 0, 0
199
+ for i, c := range seq {
200
+ if c == '(' {
201
+ if a < b {
202
+ a++
203
+ } else {
204
+ b++
205
+ ans[i] = 1
206
+ }
207
+ } else {
208
+ if a > b {
209
+ a--
210
+ } else {
211
+ b--
212
+ ans[i] = 1
213
+ }
214
+ }
215
+ }
216
+ return ans
217
+ }
105
218
```
106
219
107
220
### ** ...**
Original file line number Diff line number Diff line change 59
59
### ** Python3**
60
60
61
61
``` python
62
-
62
+ class Solution :
63
+ def maxDepthAfterSplit (self , seq : str ) -> List[int ]:
64
+ ans = [0 ] * len (seq)
65
+ a = b = 0
66
+ for i, c in enumerate (seq):
67
+ if c == " (" :
68
+ if a < b:
69
+ a += 1
70
+ else :
71
+ b += 1
72
+ ans[i] = 1
73
+ else :
74
+ if a > b:
75
+ a -= 1
76
+ else :
77
+ b -= 1
78
+ ans[i] = 1
79
+ return ans
63
80
```
64
81
65
82
### ** Java**
66
83
67
84
``` java
85
+ class Solution {
86
+ public int [] maxDepthAfterSplit (String seq ) {
87
+ int [] res = new int [seq. length()];
88
+ for (int i = 0 , cnt = 0 ; i < res. length; ++ i) {
89
+ if (seq. charAt(i) == ' (' ) {
90
+ res[i] = cnt++ & 1 ;
91
+ } else {
92
+ res[i] = -- cnt & 1 ;
93
+ }
94
+ }
95
+ return res;
96
+ }
97
+ }
98
+ ```
99
+
100
+ ``` java
101
+ class Solution {
102
+ public int [] maxDepthAfterSplit (String seq ) {
103
+ int n = seq. length();
104
+ int [] ans = new int [n];
105
+ int a = 0 , b = 0 ;
106
+ for (int i = 0 ; i < n; ++ i) {
107
+ char c = seq. charAt(i);
108
+ if (c == ' (' ) {
109
+ if (a < b) {
110
+ ++ a;
111
+ } else {
112
+ ++ b;
113
+ ans[i] = 1 ;
114
+ }
115
+ } else {
116
+ if (a > b) {
117
+ -- a;
118
+ } else {
119
+ -- b;
120
+ ans[i] = 1 ;
121
+ }
122
+ }
123
+ }
124
+ return ans;
125
+ }
126
+ }
127
+ ```
128
+
129
+ ### ** C++**
130
+
131
+ ``` cpp
132
+ class Solution {
133
+ public:
134
+ vector<int > maxDepthAfterSplit(string seq) {
135
+ int n = seq.size();
136
+ vector<int > ans(n);
137
+ int a = 0, b = 0;
138
+ for (int i = 0; i < n; ++i)
139
+ {
140
+ char c = seq[ i] ;
141
+ if (c == '(')
142
+ {
143
+ if (a < b) ++a;
144
+ else ++b, ans[ i] = 1;
145
+ }
146
+ else
147
+ {
148
+ if (a > b) --a;
149
+ else --b, ans[ i] = 1;
150
+ }
151
+ }
152
+ return ans;
153
+ }
154
+ };
155
+ ```
68
156
157
+ ### **Go**
158
+
159
+ ```go
160
+ func maxDepthAfterSplit(seq string) []int {
161
+ ans := make([]int, len(seq))
162
+ a, b := 0, 0
163
+ for i, c := range seq {
164
+ if c == '(' {
165
+ if a < b {
166
+ a++
167
+ } else {
168
+ b++
169
+ ans[i] = 1
170
+ }
171
+ } else {
172
+ if a > b {
173
+ a--
174
+ } else {
175
+ b--
176
+ ans[i] = 1
177
+ }
178
+ }
179
+ }
180
+ return ans
181
+ }
69
182
```
70
183
71
184
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > maxDepthAfterSplit (string seq) {
4
+ int n = seq.size ();
5
+ vector<int > ans (n);
6
+ int a = 0 , b = 0 ;
7
+ for (int i = 0 ; i < n; ++i)
8
+ {
9
+ char c = seq[i];
10
+ if (c == ' (' )
11
+ {
12
+ if (a < b) ++a;
13
+ else ++b, ans[i] = 1 ;
14
+ }
15
+ else
16
+ {
17
+ if (a > b) --a;
18
+ else --b, ans[i] = 1 ;
19
+ }
20
+ }
21
+ return ans;
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxDepthAfterSplit (self , seq : str ) -> List [int ]:
3
+ ans = [0 ] * len (seq )
4
+ a = b = 0
5
+ for i , c in enumerate (seq ):
6
+ if c == "(" :
7
+ if a < b :
8
+ a += 1
9
+ else :
10
+ b += 1
11
+ ans [i ] = 1
12
+ else :
13
+ if a > b :
14
+ a -= 1
15
+ else :
16
+ b -= 1
17
+ ans [i ] = 1
18
+ return ans
Original file line number Diff line number Diff line change
1
+ func maxDepthAfterSplit (seq string ) []int {
2
+ ans := make ([]int , len (seq ))
3
+ a , b := 0 , 0
4
+ for i , c := range seq {
5
+ if c == '(' {
6
+ if a < b {
7
+ a ++
8
+ } else {
9
+ b ++
10
+ ans [i ] = 1
11
+ }
12
+ } else {
13
+ if a > b {
14
+ a --
15
+ } else {
16
+ b --
17
+ ans [i ] = 1
18
+ }
19
+ }
20
+ }
21
+ return ans
22
+ }
You can’t perform that action at this time.
0 commit comments