Skip to content

Commit cefb9f4

Browse files
committed
feat: add solutions to lc problem: No.1180
No.1180.Count Substrings with Only One Distinct Letter
1 parent 5d8fd0a commit cefb9f4

File tree

12 files changed

+221
-22
lines changed

12 files changed

+221
-22
lines changed

solution/0400-0499/0417.Pacific Atlantic Water Flow/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ class Solution:
8282
q2.append((i, j))
8383
bfs(q1, vis1)
8484
bfs(q2, vis2)
85-
ans = []
86-
for i in range(m):
87-
for j in range(n):
88-
if (i, j) in vis1 and (i, j) in vis2:
89-
ans.append((i, j))
90-
return ans
85+
return [(i, j) for i in range(m) for j in range(n) if (i, j) in vis1 and (i, j) in vis2]
9186
```
9287

9388
### **Java**

solution/0400-0499/0417.Pacific Atlantic Water Flow/README_EN.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ class Solution:
7070
q2.append((i, j))
7171
bfs(q1, vis1)
7272
bfs(q2, vis2)
73-
ans = []
74-
for i in range(m):
75-
for j in range(n):
76-
if (i, j) in vis1 and (i, j) in vis2:
77-
ans.append((i, j))
78-
return ans
73+
return [(i, j) for i in range(m) for j in range(n) if (i, j) in vis1 and (i, j) in vis2]
7974
```
8075

8176
### **Java**

solution/0400-0499/0417.Pacific Atlantic Water Flow/Solution.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def bfs(q, vis):
2929
q2.append((i, j))
3030
bfs(q1, vis1)
3131
bfs(q2, vis2)
32-
ans = []
33-
for i in range(m):
34-
for j in range(n):
35-
if (i, j) in vis1 and (i, j) in vis2:
36-
ans.append((i, j))
37-
return ans
32+
return [
33+
(i, j)
34+
for i in range(m)
35+
for j in range(n)
36+
if (i, j) in vis1 and (i, j) in vis2
37+
]

solution/0700-0799/0785.Is Graph Bipartite/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ d[find(a)] = distance
124124

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

127+
染色法:
128+
127129
```python
128130
class Solution:
129131
def isBipartite(self, graph: List[List[int]]) -> bool:
@@ -145,6 +147,8 @@ class Solution:
145147
return True
146148
```
147149

150+
并查集:
151+
148152
```python
149153
class Solution:
150154
def isBipartite(self, graph: List[List[int]]) -> bool:
@@ -166,6 +170,8 @@ class Solution:
166170

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

173+
染色法:
174+
169175
```java
170176
class Solution {
171177
private int[] color;
@@ -199,6 +205,8 @@ class Solution {
199205
}
200206
```
201207

208+
并查集:
209+
202210
```java
203211
class Solution {
204212
private int[] p;
@@ -232,6 +240,8 @@ class Solution {
232240

233241
### **C++**
234242

243+
染色法:
244+
235245
```cpp
236246
class Solution {
237247
public:
@@ -259,6 +269,8 @@ public:
259269
};
260270
```
261271

272+
并查集:
273+
262274
```cpp
263275
class Solution {
264276
public:
@@ -289,6 +301,8 @@ public:
289301
290302
### **Go**
291303
304+
染色法:
305+
292306
```go
293307
func isBipartite(graph [][]int) bool {
294308
n := len(graph)
@@ -316,6 +330,8 @@ func isBipartite(graph [][]int) bool {
316330
}
317331
```
318332

333+
并查集:
334+
319335
```go
320336
func isBipartite(graph [][]int) bool {
321337
n := len(graph)
@@ -344,6 +360,8 @@ func isBipartite(graph [][]int) bool {
344360

345361
### **TypeScript**
346362

363+
染色法:
364+
347365
```ts
348366
function isBipartite(graph: number[][]): boolean {
349367
const n = graph.length;
@@ -373,6 +391,8 @@ function isBipartite(graph: number[][]): boolean {
373391
}
374392
```
375393

394+
并查集:
395+
376396
```ts
377397
function isBipartite(graph: number[][]): boolean {
378398
const n = graph.length;

solution/0700-0799/0785.Is Graph Bipartite/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
### **Python3**
5353

54+
Graph coloring:
55+
5456
```python
5557
class Solution:
5658
def isBipartite(self, graph: List[List[int]]) -> bool:
@@ -72,6 +74,8 @@ class Solution:
7274
return True
7375
```
7476

77+
Union find:
78+
7579
```python
7680
class Solution:
7781
def isBipartite(self, graph: List[List[int]]) -> bool:
@@ -91,6 +95,8 @@ class Solution:
9195

9296
### **Java**
9397

98+
Graph coloring:
99+
94100
```java
95101
class Solution {
96102
private int[] color;
@@ -124,6 +130,8 @@ class Solution {
124130
}
125131
```
126132

133+
Union find:
134+
127135
```java
128136
class Solution {
129137
private int[] p;
@@ -157,6 +165,8 @@ class Solution {
157165

158166
### **C++**
159167

168+
Graph coloring:
169+
160170
```cpp
161171
class Solution {
162172
public:
@@ -184,6 +194,8 @@ public:
184194
};
185195
```
186196

197+
Union find:
198+
187199
```cpp
188200
class Solution {
189201
public:
@@ -214,6 +226,8 @@ public:
214226
215227
### **Go**
216228
229+
Graph coloring:
230+
217231
```go
218232
func isBipartite(graph [][]int) bool {
219233
n := len(graph)
@@ -241,6 +255,8 @@ func isBipartite(graph [][]int) bool {
241255
}
242256
```
243257

258+
Union find:
259+
244260
```go
245261
func isBipartite(graph [][]int) bool {
246262
n := len(graph)
@@ -269,6 +285,8 @@ func isBipartite(graph [][]int) bool {
269285

270286
### **TypeScript**
271287

288+
Graph coloring:
289+
272290
```ts
273291
function isBipartite(graph: number[][]): boolean {
274292
const n = graph.length;
@@ -297,6 +315,8 @@ function isBipartite(graph: number[][]): boolean {
297315
}
298316
```
299317

318+
Union find:
319+
300320
```ts
301321
function isBipartite(graph: number[][]): boolean {
302322
const n = graph.length;

solution/1100-1199/1180.Count Substrings with Only One Distinct Letter/README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,74 @@
4848
<!-- 这里可写当前语言的特殊实现逻辑 -->
4949

5050
```python
51-
51+
class Solution:
52+
def countLetters(self, s: str) -> int:
53+
n = len(s)
54+
i = ans = 0
55+
while i < n:
56+
j = i
57+
while j < n and s[j] == s[i]:
58+
j += 1
59+
ans += (1 + j - i) * (j - i) // 2
60+
i = j
61+
return ans
5262
```
5363

5464
### **Java**
5565

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

5868
```java
69+
class Solution {
70+
public int countLetters(String s) {
71+
int ans = 0;
72+
for (int i = 0, n = s.length(); i < n;) {
73+
int j = i;
74+
while (j < n && s.charAt(j) == s.charAt(i)) {
75+
++j;
76+
}
77+
ans += (1 + j - i) * (j - i) / 2;
78+
i = j;
79+
}
80+
return ans;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int countLetters(string s) {
91+
int ans = 0;
92+
for (int i = 0, n = s.size(); i < n;)
93+
{
94+
int j = i;
95+
while (j < n && s[j] == s[i]) ++j;
96+
ans += (1 + j - i) * (j - i) / 2;
97+
i = j;
98+
}
99+
return ans;
100+
}
101+
};
102+
```
59103
104+
### **Go**
105+
106+
```go
107+
func countLetters(s string) int {
108+
ans := 0
109+
for i, n := 0, len(s); i < n; {
110+
j := i
111+
for j < n && s[j] == s[i] {
112+
j++
113+
}
114+
ans += (1 + j - i) * (j - i) / 2
115+
i = j
116+
}
117+
return ans
118+
}
60119
```
61120

62121
### **...**

solution/1100-1199/1180.Count Substrings with Only One Distinct Letter/README_EN.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,72 @@ So the answer is 1 + 2 + 4 + 1 = 8.
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def countLetters(self, s: str) -> int:
47+
n = len(s)
48+
i = ans = 0
49+
while i < n:
50+
j = i
51+
while j < n and s[j] == s[i]:
52+
j += 1
53+
ans += (1 + j - i) * (j - i) // 2
54+
i = j
55+
return ans
4656
```
4757

4858
### **Java**
4959

5060
```java
61+
class Solution {
62+
public int countLetters(String s) {
63+
int ans = 0;
64+
for (int i = 0, n = s.length(); i < n;) {
65+
int j = i;
66+
while (j < n && s.charAt(j) == s.charAt(i)) {
67+
++j;
68+
}
69+
ans += (1 + j - i) * (j - i) / 2;
70+
i = j;
71+
}
72+
return ans;
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
int countLetters(string s) {
83+
int ans = 0;
84+
for (int i = 0, n = s.size(); i < n;)
85+
{
86+
int j = i;
87+
while (j < n && s[j] == s[i]) ++j;
88+
ans += (1 + j - i) * (j - i) / 2;
89+
i = j;
90+
}
91+
return ans;
92+
}
93+
};
94+
```
5195
96+
### **Go**
97+
98+
```go
99+
func countLetters(s string) int {
100+
ans := 0
101+
for i, n := 0, len(s); i < n; {
102+
j := i
103+
for j < n && s[j] == s[i] {
104+
j++
105+
}
106+
ans += (1 + j - i) * (j - i) / 2
107+
i = j
108+
}
109+
return ans
110+
}
52111
```
53112

54113
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int countLetters(string s) {
4+
int ans = 0;
5+
for (int i = 0, n = s.size(); i < n;)
6+
{
7+
int j = i;
8+
while (j < n && s[j] == s[i]) ++j;
9+
ans += (1 + j - i) * (j - i) / 2;
10+
i = j;
11+
}
12+
return ans;
13+
}
14+
};

0 commit comments

Comments
 (0)