Skip to content

Commit 2604173

Browse files
committed
feat: add solutions to lc problems: No.1784,2078
* No.1784.Check if Binary String Has at Most One Segment of Ones * No.2078.Two Furthest Houses With Different Colors
1 parent 2defbeb commit 2604173

File tree

15 files changed

+3613
-2773
lines changed

15 files changed

+3613
-2773
lines changed

solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,61 @@
4040

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

43+
**方法一:0 后面不能有 1**
44+
45+
注意到字符串 $s$ 不含前导零,说明 $s$ 以 "1" 开头,若字符串后面出现 "01",则不满足题意。
46+
4347
<!-- tabs:start -->
4448

4549
### **Python3**
4650

4751
<!-- 这里可写当前语言的特殊实现逻辑 -->
4852

4953
```python
54+
class Solution:
55+
def checkOnesSegment(self, s: str) -> bool:
56+
for i, c in enumerate(s):
57+
if c == '0':
58+
if s[:i].count('1') and s[i + 1:].count('1'):
59+
return False
60+
return True
61+
```
5062

63+
```python
64+
class Solution:
65+
def checkOnesSegment(self, s: str) -> bool:
66+
return '01' not in s
5167
```
5268

5369
### **Java**
5470

5571
<!-- 这里可写当前语言的特殊实现逻辑 -->
5672

5773
```java
74+
class Solution {
75+
public boolean checkOnesSegment(String s) {
76+
return !s.contains("01");
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
bool checkOnesSegment(string s) {
87+
return s.find("01") == -1;
88+
}
89+
};
90+
```
91+
92+
### **Go**
5893
94+
```go
95+
func checkOnesSegment(s string) bool {
96+
return !strings.Contains(s, "01")
97+
}
5998
```
6099

61100
### **...**

solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README_EN.md

+35
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,48 @@
3737
### **Python3**
3838

3939
```python
40+
class Solution:
41+
def checkOnesSegment(self, s: str) -> bool:
42+
for i, c in enumerate(s):
43+
if c == '0':
44+
if s[:i].count('1') and s[i + 1:].count('1'):
45+
return False
46+
return True
47+
```
4048

49+
```python
50+
class Solution:
51+
def checkOnesSegment(self, s: str) -> bool:
52+
return '01' not in s
4153
```
4254

4355
### **Java**
4456

4557
```java
58+
class Solution {
59+
public boolean checkOnesSegment(String s) {
60+
return !s.contains("01");
61+
}
62+
}
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
class Solution {
69+
public:
70+
bool checkOnesSegment(string s) {
71+
return s.find("01") == -1;
72+
}
73+
};
74+
```
75+
76+
### **Go**
4677
78+
```go
79+
func checkOnesSegment(s string) bool {
80+
return !strings.Contains(s, "01")
81+
}
4782
```
4883

4984
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool checkOnesSegment(string s) {
4+
return s.find("01") == -1;
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func checkOnesSegment(s string) bool {
2+
return !strings.Contains(s, "01")
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public boolean checkOnesSegment(String s) {
3+
return !s.contains("01");
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def checkOnesSegment(self, s: str) -> bool:
3+
return '01' not in s

solution/2000-2099/2078.Two Furthest Houses With Different Colors/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363

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

66+
**方法一:暴力枚举**
67+
68+
时间复杂度 $O(n^2)$。
69+
70+
**方法二:贪心**
71+
6672
<!-- tabs:start -->
6773

6874
### **Python3**
@@ -80,6 +86,20 @@ class Solution:
8086
return ans
8187
```
8288

89+
```python
90+
class Solution:
91+
def maxDistance(self, colors: List[int]) -> int:
92+
n = len(colors)
93+
if colors[0] != colors[-1]:
94+
return n - 1
95+
i, j = 1, n - 2
96+
while colors[i] == colors[0]:
97+
i += 1
98+
while colors[j] == colors[0]:
99+
j -= 1
100+
return max(n - i - 1, j)
101+
```
102+
83103
### **Java**
84104

85105
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -100,6 +120,21 @@ class Solution {
100120
}
101121
```
102122

123+
```java
124+
class Solution {
125+
public int maxDistance(int[] colors) {
126+
int n = colors.length;
127+
if (colors[0] != colors[n - 1]) {
128+
return n - 1;
129+
}
130+
int i = 0, j = n - 1;
131+
while (colors[++i] == colors[0]);
132+
while (colors[--j] == colors[0]);
133+
return Math.max(n - i - 1, j);
134+
}
135+
}
136+
```
137+
103138
### **C++**
104139

105140
```cpp
@@ -116,6 +151,20 @@ public:
116151
};
117152
```
118153
154+
```cpp
155+
class Solution {
156+
public:
157+
int maxDistance(vector<int>& colors) {
158+
int n = colors.size();
159+
if (colors[0] != colors[n - 1]) return n - 1;
160+
int i = 0, j = n;
161+
while (colors[++i] == colors[0]);
162+
while (colors[--j] == colors[0]);
163+
return max(n - i - 1, j);
164+
}
165+
};
166+
```
167+
119168
### **Go**
120169

121170
```go
@@ -146,6 +195,30 @@ func abs(x int) int {
146195
}
147196
```
148197

198+
```go
199+
func maxDistance(colors []int) int {
200+
n := len(colors)
201+
if colors[0] != colors[n-1] {
202+
return n - 1
203+
}
204+
i, j := 1, n-2
205+
for colors[i] == colors[0] {
206+
i++
207+
}
208+
for colors[j] == colors[0] {
209+
j--
210+
}
211+
return max(n-i-1, j)
212+
}
213+
214+
func max(a, b int) int {
215+
if a > b {
216+
return a
217+
}
218+
return b
219+
}
220+
```
221+
149222
### **...**
150223

151224
```

solution/2000-2099/2078.Two Furthest Houses With Different Colors/README_EN.md

+67
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ class Solution:
6868
return ans
6969
```
7070

71+
```python
72+
class Solution:
73+
def maxDistance(self, colors: List[int]) -> int:
74+
n = len(colors)
75+
if colors[0] != colors[-1]:
76+
return n - 1
77+
i, j = 1, n - 2
78+
while colors[i] == colors[0]:
79+
i += 1
80+
while colors[j] == colors[0]:
81+
j -= 1
82+
return max(n - i - 1, j)
83+
```
84+
7185
### **Java**
7286

7387
```java
@@ -86,6 +100,21 @@ class Solution {
86100
}
87101
```
88102

103+
```java
104+
class Solution {
105+
public int maxDistance(int[] colors) {
106+
int n = colors.length;
107+
if (colors[0] != colors[n - 1]) {
108+
return n - 1;
109+
}
110+
int i = 0, j = n - 1;
111+
while (colors[++i] == colors[0]);
112+
while (colors[--j] == colors[0]);
113+
return Math.max(n - i - 1, j);
114+
}
115+
}
116+
```
117+
89118
### **C++**
90119

91120
```cpp
@@ -102,6 +131,20 @@ public:
102131
};
103132
```
104133
134+
```cpp
135+
class Solution {
136+
public:
137+
int maxDistance(vector<int>& colors) {
138+
int n = colors.size();
139+
if (colors[0] != colors[n - 1]) return n - 1;
140+
int i = 0, j = n;
141+
while (colors[++i] == colors[0]);
142+
while (colors[--j] == colors[0]);
143+
return max(n - i - 1, j);
144+
}
145+
};
146+
```
147+
105148
### **Go**
106149

107150
```go
@@ -132,6 +175,30 @@ func abs(x int) int {
132175
}
133176
```
134177

178+
```go
179+
func maxDistance(colors []int) int {
180+
n := len(colors)
181+
if colors[0] != colors[n-1] {
182+
return n - 1
183+
}
184+
i, j := 1, n-2
185+
for colors[i] == colors[0] {
186+
i++
187+
}
188+
for colors[j] == colors[0] {
189+
j--
190+
}
191+
return max(n-i-1, j)
192+
}
193+
194+
func max(a, b int) int {
195+
if a > b {
196+
return a
197+
}
198+
return b
199+
}
200+
```
201+
135202
### **...**
136203

137204
```
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public:
33
int maxDistance(vector<int>& colors) {
4-
int ans = 0, n = colors.size();
5-
for (int i = 0; i < n; ++i)
6-
for (int j = i + 1; j < n; ++j)
7-
if (colors[i] != colors[j])
8-
ans = max(ans, abs(i - j));
9-
return ans;
4+
int n = colors.size();
5+
if (colors[0] != colors[n - 1]) return n - 1;
6+
int i = 0, j = n;
7+
while (colors[++i] == colors[0]);
8+
while (colors[--j] == colors[0]);
9+
return max(n - i - 1, j);
1010
}
1111
};
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
func maxDistance(colors []int) int {
2-
ans, n := 0, len(colors)
3-
for i := 0; i < n; i++ {
4-
for j := i + 1; j < n; j++ {
5-
if colors[i] != colors[j] {
6-
ans = max(ans, abs(i-j))
7-
}
8-
}
2+
n := len(colors)
3+
if colors[0] != colors[n-1] {
4+
return n - 1
95
}
10-
return ans
6+
i, j := 1, n-2
7+
for colors[i] == colors[0] {
8+
i++
9+
}
10+
for colors[j] == colors[0] {
11+
j--
12+
}
13+
return max(n-i-1, j)
1114
}
1215

1316
func max(a, b int) int {
1417
if a > b {
1518
return a
1619
}
1720
return b
18-
}
19-
20-
func abs(x int) int {
21-
if x >= 0 {
22-
return x
23-
}
24-
return -x
2521
}

0 commit comments

Comments
 (0)