Skip to content

Commit 89d6e71

Browse files
committed
feat: add solutions to lc problem: No.1869.Longer Contiguous Segments of Ones than Zeros
1 parent b680b61 commit 89d6e71

File tree

6 files changed

+188
-30
lines changed

6 files changed

+188
-30
lines changed

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

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
```python
7676
class Solution:
7777
def checkZeroOnes(self, s: str) -> bool:
78-
len0 = len1 = 0
78+
n0 = n1 = 0
7979
t0 = t1 = 0
8080
for c in s:
8181
if c == '0':
@@ -84,9 +84,9 @@ class Solution:
8484
else:
8585
t0 = 0
8686
t1 += 1
87-
len0 = max(len0, t0)
88-
len1 = max(len1, t1)
89-
return len1 > len0
87+
n0 = max(n0, t0)
88+
n1 = max(n1, t1)
89+
return n1 > n0
9090
```
9191

9292
### **Java**
@@ -96,20 +96,20 @@ class Solution:
9696
```java
9797
class Solution {
9898
public boolean checkZeroOnes(String s) {
99-
int len0 = 0, len1 = 0;
99+
int n0 = 0, n1 = 0;
100100
int t0 = 0, t1 = 0;
101101
for (int i = 0; i < s.length(); ++i) {
102102
if (s.charAt(i) == '0') {
103-
t0 += 1;
103+
++t0;
104104
t1 = 0;
105105
} else {
106+
++t1;
106107
t0 = 0;
107-
t1 += 1;
108108
}
109-
len0 = Math.max(len0, t0);
110-
len1 = Math.max(len1, t1);
109+
n0 = Math.max(n0, t0);
110+
n1 = Math.max(n1, t1);
111111
}
112-
return len1 > len0;
112+
return n1 > n0;
113113
}
114114
}
115115
```
@@ -139,6 +139,62 @@ class Solution {
139139
};
140140
```
141141

142+
### **C++**
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
bool checkZeroOnes(string s) {
148+
int n0 = 0, n1 = 0;
149+
int t0 = 0, t1 = 0;
150+
for (auto c : s)
151+
{
152+
if (c == '0')
153+
{
154+
++t0;
155+
t1 = 0;
156+
}
157+
else
158+
{
159+
++t1;
160+
t0 = 0;
161+
}
162+
n0 = max(n0, t0);
163+
n1 = max(n1, t1);
164+
}
165+
return n1 > n0;
166+
}
167+
};
168+
```
169+
170+
### **Go**
171+
172+
```go
173+
func checkZeroOnes(s string) bool {
174+
n0, n1 := 0, 0
175+
t0, t1 := 0, 0
176+
for _, c := range s {
177+
if c == '0' {
178+
t0++
179+
t1 = 0
180+
} else {
181+
t1++
182+
t0 = 0
183+
}
184+
n0 = max(n0, t0)
185+
n1 = max(n1, t1)
186+
}
187+
return n1 > n0
188+
}
189+
190+
func max(a, b int) int {
191+
if a > b {
192+
return a
193+
}
194+
return b
195+
}
196+
```
197+
142198
### **...**
143199

144200
```

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

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The segment of 1s is not longer, so return false.
6363
```python
6464
class Solution:
6565
def checkZeroOnes(self, s: str) -> bool:
66-
len0 = len1 = 0
66+
n0 = n1 = 0
6767
t0 = t1 = 0
6868
for c in s:
6969
if c == '0':
@@ -72,30 +72,30 @@ class Solution:
7272
else:
7373
t0 = 0
7474
t1 += 1
75-
len0 = max(len0, t0)
76-
len1 = max(len1, t1)
77-
return len1 > len0
75+
n0 = max(n0, t0)
76+
n1 = max(n1, t1)
77+
return n1 > n0
7878
```
7979

8080
### **Java**
8181

8282
```java
8383
class Solution {
8484
public boolean checkZeroOnes(String s) {
85-
int len0 = 0, len1 = 0;
85+
int n0 = 0, n1 = 0;
8686
int t0 = 0, t1 = 0;
8787
for (int i = 0; i < s.length(); ++i) {
8888
if (s.charAt(i) == '0') {
89-
t0 += 1;
89+
++t0;
9090
t1 = 0;
9191
} else {
92+
++t1;
9293
t0 = 0;
93-
t1 += 1;
9494
}
95-
len0 = Math.max(len0, t0);
96-
len1 = Math.max(len1, t1);
95+
n0 = Math.max(n0, t0);
96+
n1 = Math.max(n1, t1);
9797
}
98-
return len1 > len0;
98+
return n1 > n0;
9999
}
100100
}
101101
```
@@ -125,6 +125,62 @@ class Solution {
125125
};
126126
```
127127

128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
bool checkZeroOnes(string s) {
134+
int n0 = 0, n1 = 0;
135+
int t0 = 0, t1 = 0;
136+
for (auto c : s)
137+
{
138+
if (c == '0')
139+
{
140+
++t0;
141+
t1 = 0;
142+
}
143+
else
144+
{
145+
++t1;
146+
t0 = 0;
147+
}
148+
n0 = max(n0, t0);
149+
n1 = max(n1, t1);
150+
}
151+
return n1 > n0;
152+
}
153+
};
154+
```
155+
156+
### **Go**
157+
158+
```go
159+
func checkZeroOnes(s string) bool {
160+
n0, n1 := 0, 0
161+
t0, t1 := 0, 0
162+
for _, c := range s {
163+
if c == '0' {
164+
t0++
165+
t1 = 0
166+
} else {
167+
t1++
168+
t0 = 0
169+
}
170+
n0 = max(n0, t0)
171+
n1 = max(n1, t1)
172+
}
173+
return n1 > n0
174+
}
175+
176+
func max(a, b int) int {
177+
if a > b {
178+
return a
179+
}
180+
return b
181+
}
182+
```
183+
128184
### **...**
129185

130186
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool checkZeroOnes(string s) {
4+
int n0 = 0, n1 = 0;
5+
int t0 = 0, t1 = 0;
6+
for (auto c : s)
7+
{
8+
if (c == '0')
9+
{
10+
++t0;
11+
t1 = 0;
12+
}
13+
else
14+
{
15+
++t1;
16+
t0 = 0;
17+
}
18+
n0 = max(n0, t0);
19+
n1 = max(n1, t1);
20+
}
21+
return n1 > n0;
22+
}
23+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func checkZeroOnes(s string) bool {
2+
n0, n1 := 0, 0
3+
t0, t1 := 0, 0
4+
for _, c := range s {
5+
if c == '0' {
6+
t0++
7+
t1 = 0
8+
} else {
9+
t1++
10+
t0 = 0
11+
}
12+
n0 = max(n0, t0)
13+
n1 = max(n1, t1)
14+
}
15+
return n1 > n0
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
class Solution {
22
public boolean checkZeroOnes(String s) {
3-
int len0 = 0, len1 = 0;
3+
int n0 = 0, n1 = 0;
44
int t0 = 0, t1 = 0;
55
for (int i = 0; i < s.length(); ++i) {
66
if (s.charAt(i) == '0') {
7-
t0 += 1;
7+
++t0;
88
t1 = 0;
99
} else {
10+
++t1;
1011
t0 = 0;
11-
t1 += 1;
1212
}
13-
len0 = Math.max(len0, t0);
14-
len1 = Math.max(len1, t1);
13+
n0 = Math.max(n0, t0);
14+
n1 = Math.max(n1, t1);
1515
}
16-
return len1 > len0;
16+
return n1 > n0;
1717
}
1818
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def checkZeroOnes(self, s: str) -> bool:
3-
len0 = len1 = 0
3+
n0 = n1 = 0
44
t0 = t1 = 0
55
for c in s:
66
if c == '0':
@@ -9,6 +9,6 @@ def checkZeroOnes(self, s: str) -> bool:
99
else:
1010
t0 = 0
1111
t1 += 1
12-
len0 = max(len0, t0)
13-
len1 = max(len1, t1)
14-
return len1 > len0
12+
n0 = max(n0, t0)
13+
n1 = max(n1, t1)
14+
return n1 > n0

0 commit comments

Comments
 (0)