Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problems: No.1869~1872 #1980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
acbin merged 1 commit into main from dev
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@

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

直接遍历字符串,获取"0 子串"和"1 子串"的最大长度 `len0`、`len1`。
**方法一:两次遍历**

遍历结束后,若 `len1 > len0`,返回 true,否则返回 false。
我们设计一个函数 $f(x),ドル表示字符串 $s$ 中由 $x$ 组成的最长连续子字符串的长度。如果 $f(1) \gt f(0),ドル那么返回 `true`,否则返回 `false`。

时间复杂度 $O(n),ドル其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -75,18 +77,17 @@
```python
class Solution:
def checkZeroOnes(self, s: str) -> bool:
n0 = n1 = 0
t0 = t1 = 0
for c in s:
if c == '0':
t0 += 1
t1 = 0
else:
t0 = 0
t1 += 1
n0 = max(n0, t0)
n1 = max(n1, t1)
return n1 > n0
def f(x: str) -> int:
cnt = mx = 0
for c in s:
if c == x:
cnt += 1
mx = max(mx, cnt)
else:
cnt = 0
return mx

return f("1") > f("0")
```

### **Java**
Expand All @@ -96,71 +97,41 @@ class Solution:
```java
class Solution {
public boolean checkZeroOnes(String s) {
int n0 = 0, n1 = 0;
int t0 = 0, t1 = 0;
return f(s, '1') > f(s, '0');
}

private int f(String s, char x) {
int cnt = 0, mx = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '0') {
++t0;
t1 = 0;
if (s.charAt(i) == x) {
mx = Math.max(mx, ++cnt);
} else {
++t1;
t0 = 0;
cnt = 0;
}
n0 = Math.max(n0, t0);
n1 = Math.max(n1, t1);
}
return n1 > n0;
return mx;
}
}
```

### **JavaScript**

```js
/**
* @param {string} s
* @return {boolean}
*/
var checkZeroOnes = function (s) {
let max0 = 0,
max1 = 0;
let t0 = 0,
t1 = 0;
for (let char of s) {
if (char == '0') {
t0++;
t1 = 0;
} else {
t1++;
t0 = 0;
}
max0 = Math.max(max0, t0);
max1 = Math.max(max1, t1);
}
return max1 > max0;
};
```

### **C++**

```cpp
class Solution {
public:
bool checkZeroOnes(string s) {
int n0 = 0, n1 = 0;
int t0 = 0, t1 = 0;
for (auto c : s) {
if (c == '0') {
++t0;
t1 = 0;
} else {
++t1;
t0 = 0;
auto f = [&](char x) {
int cnt = 0, mx = 0;
for (char& c : s) {
if (c == x) {
mx = max(mx, ++cnt);
} else {
cnt = 0;
}
}
n0 = max(n0, t0);
n1 = max(n1, t1);
}
return n1 > n0;
return mx;
};
return f('1') > f('0');
}
};
```
Expand All @@ -169,23 +140,64 @@ public:

```go
func checkZeroOnes(s string) bool {
n0, n1 := 0, 0
t0, t1 := 0, 0
for _, c := range s {
if c == '0' {
t0++
t1 = 0
} else {
t1++
t0 = 0
f := func(x rune) int {
cnt, mx := 0, 0
for _, c := range s {
if c == x {
cnt++
mx = max(mx, cnt)
} else {
cnt = 0
}
}
n0 = max(n0, t0)
n1 = max(n1, t1)
return mx
}
return n1 > n0
return f('1') > f('0')
}
```

### **TypeScript**

```ts
function checkZeroOnes(s: string): boolean {
const f = (x: string): number => {
let [mx, cnt] = [0, 0];
for (const c of s) {
if (c === x) {
mx = Math.max(mx, ++cnt);
} else {
cnt = 0;
}
}
return mx;
};
return f('1') > f('0');
}
```

### **JavaScript**

```js
/**
* @param {string} s
* @return {boolean}
*/
var checkZeroOnes = function (s) {
const f = x => {
let [mx, cnt] = [0, 0];
for (const c of s) {
if (c === x) {
mx = Math.max(mx, ++cnt);
} else {
cnt = 0;
}
}
return mx;
};
return f('1') > f('0');
};
```

### **...**

```
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /