Skip to content

Commit a5f3cb0

Browse files
authored
feat: add solutions to lc problems: No.1869~1872 (#1980)
* No.1869.Longer Contiguous Segments of Ones than Zeros * No.1871.Jump Game VII * No.1872.Stone Game VIII
1 parent dd7aff3 commit a5f3cb0

23 files changed

+1017
-389
lines changed

solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README.md

+88-76
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65-
直接遍历字符串,获取“0 子串”和“1 子串”的最大长度 `len0``len1`
65+
**方法一:两次遍历**
6666

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)$。
6870

6971
<!-- tabs:start -->
7072

@@ -75,18 +77,17 @@
7577
```python
7678
class Solution:
7779
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")
9091
```
9192

9293
### **Java**
@@ -96,71 +97,41 @@ class Solution:
9697
```java
9798
class Solution {
9899
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;
101105
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);
105108
} else {
106-
++t1;
107-
t0 = 0;
109+
cnt = 0;
108110
}
109-
n0 = Math.max(n0, t0);
110-
n1 = Math.max(n1, t1);
111111
}
112-
return n1 > n0;
112+
return mx;
113113
}
114114
}
115115
```
116116

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-
144117
### **C++**
145118

146119
```cpp
147120
class Solution {
148121
public:
149122
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+
}
159131
}
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');
164135
}
165136
};
166137
```
@@ -169,23 +140,64 @@ public:
169140
170141
```go
171142
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+
}
181152
}
182-
n0 = max(n0, t0)
183-
n1 = max(n1, t1)
153+
return mx
184154
}
185-
return n1 > n0
155+
return f('1') > f('0')
186156
}
187157
```
188158

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+
189201
### **...**
190202

191203
```

0 commit comments

Comments
 (0)