@@ -28,13 +28,13 @@ tags:
28
28
<p ><strong >示例 1:</strong ></p >
29
29
30
30
<pre ><strong >输入:</strong >s = " ; 011101" ;
31
- <strong >输出:</strong >5
31
+ <strong >输出:</strong >5
32
32
<strong >解释:</strong >
33
33
将字符串 s 划分为两个非空子字符串的可行方案有:
34
- 左子字符串 = " ; 0" ; 且 右子字符串 = " ; 11101" ; ,得分 = 1 + 4 = 5
35
- 左子字符串 = " ; 01" ; 且 右子字符串 = " ; 1101" ; ,得分 = 1 + 3 = 4
36
- 左子字符串 = " ; 011" ; 且 右子字符串 = " ; 101" ; ,得分 = 1 + 2 = 3
37
- 左子字符串 = " ; 0111" ; 且 右子字符串 = " ; 01" ; ,得分 = 1 + 1 = 2
34
+ 左子字符串 = " ; 0" ; 且 右子字符串 = " ; 11101" ; ,得分 = 1 + 4 = 5
35
+ 左子字符串 = " ; 01" ; 且 右子字符串 = " ; 1101" ; ,得分 = 1 + 3 = 4
36
+ 左子字符串 = " ; 011" ; 且 右子字符串 = " ; 101" ; ,得分 = 1 + 2 = 3
37
+ 左子字符串 = " ; 0111" ; 且 右子字符串 = " ; 01" ; ,得分 = 1 + 1 = 2
38
38
左子字符串 = " ; 01110" ; 且 右子字符串 = " ; 1" ; ,得分 = 2 + 1 = 3
39
39
</pre >
40
40
66
66
67
67
<!-- solution:start -->
68
68
69
- ### 方法一
69
+ ### 方法一:计数
70
+
71
+ 我们用两个变量 $l$ 和 $r$ 分别记录左子字符串中 $0$ 的数量和右子字符串中 $1$ 的数量。初始时 $l = 0$,而 $r$ 则等于字符串 $s$ 中 $1$ 的数量。
72
+
73
+ 遍历字符串 $s$ 的前 $n - 1$ 个字符,对于每一个位置 $i$,如果 $s[ i] = 0$,则 $l$ 自增 $1$,否则 $r$ 自减 $1$。然后我们更新答案为 $l + r$ 的最大值。
74
+
75
+ 时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
70
76
71
77
<!-- tabs:start -->
72
78
@@ -75,28 +81,32 @@ tags:
75
81
``` python
76
82
class Solution :
77
83
def maxScore (self , s : str ) -> int :
78
- return max (s[:i].count(' 0' ) + s[i:].count(' 1' ) for i in range (1 , len (s)))
84
+ l, r = 0 , s.count(" 1" )
85
+ ans = 0
86
+ for x in s[:- 1 ]:
87
+ l += int (x) ^ 1
88
+ r -= int (x)
89
+ ans = max (ans, l + r)
90
+ return ans
79
91
```
80
92
81
93
#### Java
82
94
83
95
``` java
84
96
class Solution {
85
97
public int maxScore (String s ) {
86
- int ans = 0 ;
87
- for (int i = 1 ; i < s. length(); ++ i) {
88
- int t = 0 ;
89
- for (int j = 0 ; j < i; ++ j) {
90
- if (s. charAt(j) == ' 0' ) {
91
- ++ t;
92
- }
93
- }
94
- for (int j = i; j < s. length(); ++ j) {
95
- if (s. charAt(j) == ' 1' ) {
96
- ++ t;
97
- }
98
+ int l = 0 , r = 0 ;
99
+ int n = s. length();
100
+ for (int i = 0 ; i < n; ++ i) {
101
+ if (s. charAt(i) == ' 1' ) {
102
+ ++ r;
98
103
}
99
- ans = Math . max(ans, t);
104
+ }
105
+ int ans = 0 ;
106
+ for (int i = 0 ; i < n - 1 ; ++ i) {
107
+ l += (s. charAt(i) - ' 0' ) ^ 1 ;
108
+ r -= s. charAt(i) - ' 0' ;
109
+ ans = Math . max(ans, l + r);
100
110
}
101
111
return ans;
102
112
}
@@ -109,16 +119,12 @@ class Solution {
109
119
class Solution {
110
120
public:
111
121
int maxScore(string s) {
122
+ int l = 0, r = count(s.begin(), s.end(), '1');
112
123
int ans = 0;
113
- for (int i = 1, n = s.size(); i < n; ++i) {
114
- int t = 0;
115
- for (int j = 0; j < i; ++j) {
116
- t += s[ j] == '0';
117
- }
118
- for (int j = i; j < n; ++j) {
119
- t += s[ j] == '1';
120
- }
121
- ans = max(ans, t);
124
+ for (int i = 0; i < s.size() - 1; ++i) {
125
+ l += (s[ i] - '0') ^ 1;
126
+ r -= s[ i] - '0';
127
+ ans = max(ans, l + r);
122
128
}
123
129
return ans;
124
130
}
@@ -128,51 +134,38 @@ public:
128
134
#### Go
129
135
130
136
```go
131
- func maxScore(s string) int {
132
- ans := 0
133
- for i, n := 1, len(s); i < n; i++ {
134
- t := 0
135
- for j := 0; j < i; j++ {
136
- if s[j] == '0' {
137
- t++
138
- }
139
- }
140
- for j := i; j < n; j++ {
141
- if s[j] == '1' {
142
- t++
143
- }
137
+ func maxScore(s string) (ans int) {
138
+ l, r := 0, strings.Count(s, "1")
139
+ for _, c := range s[:len(s)-1] {
140
+ if c == '0' {
141
+ l++
142
+ } else {
143
+ r--
144
144
}
145
- ans = max(ans, t )
145
+ ans = max(ans, l+r )
146
146
}
147
- return ans
147
+ return
148
148
}
149
149
```
150
150
151
151
#### TypeScript
152
152
153
153
``` ts
154
154
function maxScore(s : string ): number {
155
- const n = s .length ;
156
- let res = 0 ;
157
- let score = 0 ;
158
- if (s [0 ] === ' 0' ) {
159
- score ++ ;
155
+ let [l, r] = [0 , 0 ];
156
+ for (const c of s ) {
157
+ r += c === ' 1' ? 1 : 0 ;
160
158
}
161
- for (let i = 1 ; i < n ; i ++ ) {
162
- if (s [i ] === ' 1' ) {
163
- score ++ ;
164
- }
165
- }
166
- res = Math .max (res , score );
167
- for (let i = 1 ; i < n - 1 ; i ++ ) {
159
+ let ans = 0 ;
160
+ for (let i = 0 ; i < s .length - 1 ; ++ i ) {
168
161
if (s [i ] === ' 0' ) {
169
- score ++ ;
170
- } else if ( s [ i ] === ' 1 ' ) {
171
- score -- ;
162
+ ++ l ;
163
+ } else {
164
+ -- r ;
172
165
}
173
- res = Math .max (res , score );
166
+ ans = Math .max (ans , l + r );
174
167
}
175
- return res ;
168
+ return ans ;
176
169
}
177
170
```
178
171
@@ -181,121 +174,17 @@ function maxScore(s: string): number {
181
174
``` rust
182
175
impl Solution {
183
176
pub fn max_score (s : String ) -> i32 {
184
- let n = s . len ();
185
- let mut res = 0 ;
186
- let mut score = 0 ;
187
- let bs = s . as_bytes ();
188
- if bs [0 ] == b '0' {
189
- score += 1 ;
190
- }
191
- for i in 1 .. n {
192
- if bs [i ] == b '1' {
193
- score += 1 ;
194
- }
195
- }
196
- res = res . max (score );
197
- for i in 1 .. n - 1 {
198
- if bs [i ] == b '0' {
199
- score += 1 ;
200
- } else if bs [i ] == b '1' {
201
- score -= 1 ;
202
- }
203
- res = res . max (score );
204
- }
205
- res
206
- }
207
- }
208
- ```
209
-
210
- <!-- tabs: end -->
211
-
212
- <!-- solution: end -->
213
-
214
- <!-- solution: start -->
215
-
216
- ### 方法二
217
-
218
- <!-- tabs: start -->
219
-
220
- #### Python3
221
-
222
- ``` python
223
- class Solution :
224
- def maxScore (self , s : str ) -> int :
225
- ans = t = (s[0 ] == ' 0' ) + s[1 :].count(' 1' )
226
- for i in range (1 , len (s) - 1 ):
227
- t += 1 if s[i] == ' 0' else - 1
228
- ans = max (ans, t)
229
- return ans
230
- ```
231
-
232
- #### Java
233
-
234
- ``` java
235
- class Solution {
236
- public int maxScore (String s ) {
237
- int t = 0 ;
238
- if (s. charAt(0 ) == ' 0' ) {
239
- t++ ;
240
- }
241
- for (int i = 1 ; i < s. length(); ++ i) {
242
- if (s. charAt(i) == ' 1' ) {
243
- t++ ;
244
- }
245
- }
246
- int ans = t;
247
- for (int i = 1 ; i < s. length() - 1 ; ++ i) {
248
- t += s. charAt(i) == ' 0' ? 1 : - 1 ;
249
- ans = Math . max(ans, t);
250
- }
251
- return ans;
252
- }
253
- }
254
- ```
255
-
256
- #### C++
257
-
258
- ``` cpp
259
- class Solution {
260
- public:
261
- int maxScore(string s) {
262
- int t = 0;
263
- if (s[ 0] == '0') ++t;
264
- for (int i = 1; i < s.size(); ++i) t += s[ i] == '1';
265
- int ans = t;
266
- for (int i = 1; i < s.size() - 1; ++i) {
267
- t += s[ i] == '0' ? 1 : -1;
268
- ans = max(ans, t);
177
+ let mut l = 0 ;
178
+ let mut r = s . bytes (). filter (| & b | b == b '1' ). count () as i32 ;
179
+ let mut ans = 0 ;
180
+ let cs = s . as_bytes ();
181
+ for i in 0 .. s . len () - 1 {
182
+ l += ((cs [i ] - b '0' ) ^ 1 ) as i32 ;
183
+ r -= (cs [i ] - b '0' ) as i32 ;
184
+ ans = ans . max (l + r );
269
185
}
270
- return ans;
186
+ ans
271
187
}
272
- };
273
- ```
274
-
275
- #### Go
276
-
277
- ```go
278
- func maxScore(s string) int {
279
- t := 0
280
- if s[0] == '0' {
281
- t++
282
- }
283
- n := len(s)
284
- for i := 1; i < n; i++ {
285
- if s[i] == '1' {
286
- t++
287
- }
288
- }
289
- ans := t
290
- for i := 1; i < n-1; i++ {
291
- if s[i] == '0' {
292
- t++
293
- } else {
294
- t--
295
- }
296
- ans = max(ans, t)
297
- }
298
- return ans
299
188
}
300
189
```
301
190
0 commit comments