62
62
63
63
<!-- 这里可写通用的实现逻辑 -->
64
64
65
- 直接遍历字符串,获取“0 子串”和“1 子串”的最大长度 ` len0 ` 、 ` len1 ` 。
65
+ ** 方法一:两次遍历 **
66
66
67
- 遍历结束后,若 ` len1 > len0 ` ,返回 true,否则返回 false。
67
+ 我们设计一个函数 $f(x)$,表示字符串 $s$ 中由 $x$ 组成的最长连续子字符串的长度。如果 $f(1) \gt f(0)$,那么返回 ` true ` ,否则返回 ` false ` 。
68
+
69
+ 时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
68
70
69
71
<!-- tabs:start -->
70
72
75
77
``` python
76
78
class Solution :
77
79
def checkZeroOnes (self , s : str ) -> bool :
78
- n0 = n1 = 0
79
- t0 = t1 = 0
80
- for c in s:
81
- if c == ' 0' :
82
- t0 += 1
83
- t1 = 0
84
- else :
85
- t0 = 0
86
- t1 += 1
87
- n0 = max (n0, t0)
88
- n1 = max (n1, t1)
89
- return n1 > n0
80
+ def f (x : str ) -> int :
81
+ cnt = mx = 0
82
+ for c in s:
83
+ if c == x:
84
+ cnt += 1
85
+ mx = max (mx, cnt)
86
+ else :
87
+ cnt = 0
88
+ return mx
89
+
90
+ return f(" 1" ) > f(" 0" )
90
91
```
91
92
92
93
### ** Java**
@@ -96,71 +97,41 @@ class Solution:
96
97
``` java
97
98
class Solution {
98
99
public boolean checkZeroOnes (String s ) {
99
- int n0 = 0 , n1 = 0 ;
100
- int t0 = 0 , t1 = 0 ;
100
+ return f(s, ' 1' ) > f(s, ' 0' );
101
+ }
102
+
103
+ private int f (String s , char x ) {
104
+ int cnt = 0 , mx = 0 ;
101
105
for (int i = 0 ; i < s. length(); ++ i) {
102
- if (s. charAt(i) == ' 0' ) {
103
- ++ t0;
104
- t1 = 0 ;
106
+ if (s. charAt(i) == x) {
107
+ mx = Math . max(mx, ++ cnt);
105
108
} else {
106
- ++ t1;
107
- t0 = 0 ;
109
+ cnt = 0 ;
108
110
}
109
- n0 = Math . max(n0, t0);
110
- n1 = Math . max(n1, t1);
111
111
}
112
- return n1 > n0 ;
112
+ return mx ;
113
113
}
114
114
}
115
115
```
116
116
117
- ### ** JavaScript**
118
-
119
- ``` js
120
- /**
121
- * @param {string} s
122
- * @return {boolean}
123
- */
124
- var checkZeroOnes = function (s ) {
125
- let max0 = 0 ,
126
- max1 = 0 ;
127
- let t0 = 0 ,
128
- t1 = 0 ;
129
- for (let char of s) {
130
- if (char == ' 0' ) {
131
- t0++ ;
132
- t1 = 0 ;
133
- } else {
134
- t1++ ;
135
- t0 = 0 ;
136
- }
137
- max0 = Math .max (max0, t0);
138
- max1 = Math .max (max1, t1);
139
- }
140
- return max1 > max0;
141
- };
142
- ```
143
-
144
117
### ** C++**
145
118
146
119
``` cpp
147
120
class Solution {
148
121
public:
149
122
bool checkZeroOnes(string s) {
150
- int n0 = 0, n1 = 0;
151
- int t0 = 0, t1 = 0;
152
- for (auto c : s) {
153
- if (c == '0') {
154
- ++t0;
155
- t1 = 0;
156
- } else {
157
- ++t1;
158
- t0 = 0;
123
+ auto f = [ &] (char x) {
124
+ int cnt = 0, mx = 0;
125
+ for (char& c : s) {
126
+ if (c == x) {
127
+ mx = max(mx, ++cnt);
128
+ } else {
129
+ cnt = 0;
130
+ }
159
131
}
160
- n0 = max(n0, t0);
161
- n1 = max(n1, t1);
162
- }
163
- return n1 > n0;
132
+ return mx;
133
+ };
134
+ return f('1') > f('0');
164
135
}
165
136
};
166
137
```
@@ -169,23 +140,64 @@ public:
169
140
170
141
```go
171
142
func checkZeroOnes(s string) bool {
172
- n0, n1 := 0, 0
173
- t0, t1 := 0, 0
174
- for _, c := range s {
175
- if c == '0' {
176
- t0 ++
177
- t1 = 0
178
- } else {
179
- t1++
180
- t0 = 0
143
+ f := func(x rune) int {
144
+ cnt, mx := 0, 0
145
+ for _, c := range s {
146
+ if c == x {
147
+ cnt ++
148
+ mx = max(mx, cnt)
149
+ } else {
150
+ cnt = 0
151
+ }
181
152
}
182
- n0 = max(n0, t0)
183
- n1 = max(n1, t1)
153
+ return mx
184
154
}
185
- return n1 > n0
155
+ return f('1') > f('0')
186
156
}
187
157
```
188
158
159
+ ### ** TypeScript**
160
+
161
+ ``` ts
162
+ function checkZeroOnes(s : string ): boolean {
163
+ const f = (x : string ): number => {
164
+ let [mx, cnt] = [0 , 0 ];
165
+ for (const c of s ) {
166
+ if (c === x ) {
167
+ mx = Math .max (mx , ++ cnt );
168
+ } else {
169
+ cnt = 0 ;
170
+ }
171
+ }
172
+ return mx ;
173
+ };
174
+ return f (' 1' ) > f (' 0' );
175
+ }
176
+ ```
177
+
178
+ ### ** JavaScript**
179
+
180
+ ``` js
181
+ /**
182
+ * @param {string} s
183
+ * @return {boolean}
184
+ */
185
+ var checkZeroOnes = function (s ) {
186
+ const f = x => {
187
+ let [mx, cnt] = [0 , 0 ];
188
+ for (const c of s) {
189
+ if (c === x) {
190
+ mx = Math .max (mx, ++ cnt);
191
+ } else {
192
+ cnt = 0 ;
193
+ }
194
+ }
195
+ return mx;
196
+ };
197
+ return f (' 1' ) > f (' 0' );
198
+ };
199
+ ```
200
+
189
201
### ** ...**
190
202
191
203
```
0 commit comments