Skip to content

Commit 5f04197

Browse files
committed
feat: update solutions to lc problems: No.1247,2061,2062
* No.1247.Minimum Swaps to Make Strings Equal * No.2061.Number of Spaces Cleaning Robot Cleaned * No.2062.Count Vowel Substrings of a String
1 parent 649c994 commit 5f04197

File tree

8 files changed

+289
-82
lines changed

8 files changed

+289
-82
lines changed

solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959

6060
**方法一:贪心**
6161

62-
根据题目描述,两个字符串 $s1$ 和 $s2$ 都只包含字符 `'x'``'y'`,且长度相同,因此可以将 $s1$ 和 $s2$ 中的字符一一对应起来,即 $s1[i]$ 和 $s2[i]$。
62+
根据题目描述,两个字符串 $s1$ 和 $s2$ 都只包含字符 $x$$y$,且长度相同,因此可以将 $s1$ 和 $s2$ 中的字符一一对应起来,即 $s1[i]$ 和 $s2[i]$。
6363

64-
如果 $s1[i] = s2[i]$,则不需要交换,直接跳过即可。如果 $s1[i] \neq s2[i]$,则需要交换,我们统计 $s1[i]$ 和 $s2[i]$ 的组合情况,即 $s1[i] = 'x'$ 且 $s2[i] = 'y'$ 的情况,记为 $xy$, $s1[i] = 'y'$ 且 $s2[i] = 'x'$ 的情况,记为 $yx$。
64+
如果 $s1[i] = s2[i]$,则不需要交换,直接跳过即可。如果 $s1[i] \neq s2[i]$,则需要交换,我们统计 $s1[i]$ 和 $s2[i]$ 的组合情况,即 $s1[i] = x$ 且 $s2[i] = y$ 的情况,记为 $xy$,对于 $s1[i] = y$ 且 $s2[i] = x$ 的情况,记为 $yx$。
6565

6666
如果 $xy + yx$ 为奇数,则无法完成交换,返回 $-1$。如果 $xy + yx$ 为偶数,则需要交换的次数为 $\left \lfloor \frac{x}{2} \right \rfloor$ + $\left \lfloor \frac{y}{2} \right \rfloor$ + $xy \mod{2}$ + $yx \mod{2}$。
6767

@@ -147,13 +147,16 @@ func minimumSwap(s1 string, s2 string) int {
147147
return xy/2 + yx/2 + xy%2 + yx%2
148148
}
149149
```
150+
150151
### **JavaScript**
151152

152153
```js
153154
var minimumSwap = function (s1, s2) {
154-
let xy = 0, yx = 0;
155+
let xy = 0,
156+
yx = 0;
155157
for (let i = 0; i < s1.length; ++i) {
156-
const a = s1[i], b = s2[i];
158+
const a = s1[i],
159+
b = s2[i];
157160
if (a < b) {
158161
++xy;
159162
}
@@ -164,7 +167,7 @@ var minimumSwap = function (s1, s2) {
164167
if ((xy + yx) % 2 === 1) {
165168
return -1;
166169
}
167-
return Math.floor(xy / 2) + Math.floor(yx / 2) + xy % 2 + yx % 2;
170+
return Math.floor(xy / 2) + Math.floor(yx / 2) + (xy % 2) + (yx % 2);
168171
};
169172
```
170173

solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ func minimumSwap(s1 string, s2 string) int {
122122
return xy/2 + yx/2 + xy%2 + yx%2
123123
}
124124
```
125+
125126
### **JavaScript**
126127

127128
```js
128129
var minimumSwap = function (s1, s2) {
129-
let xy = 0, yx = 0;
130+
let xy = 0,
131+
yx = 0;
130132
for (let i = 0; i < s1.length; ++i) {
131-
const a = s1[i], b = s2[i];
133+
const a = s1[i],
134+
b = s2[i];
132135
if (a < b) {
133136
++xy;
134137
}
@@ -139,9 +142,10 @@ var minimumSwap = function (s1, s2) {
139142
if ((xy + yx) % 2 === 1) {
140143
return -1;
141144
}
142-
return Math.floor(xy / 2) + Math.floor(yx / 2) + xy % 2 + yx % 2;
145+
return Math.floor(xy / 2) + Math.floor(yx / 2) + (xy % 2) + (yx % 2);
143146
};
144147
```
148+
145149
### **...**
146150

147151
```
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var minimumSwap = function (s1, s2) {
2-
let xy = 0, yx = 0;
2+
let xy = 0,
3+
yx = 0;
34
for (let i = 0; i < s1.length; ++i) {
4-
const a = s1[i], b = s2[i];
5+
const a = s1[i],
6+
b = s2[i];
57
if (a < b) {
68
++xy;
79
}
@@ -12,5 +14,5 @@ var minimumSwap = function (s1, s2) {
1214
if ((xy + yx) % 2 === 1) {
1315
return -1;
1416
}
15-
return Math.floor(xy / 2) + Math.floor(yx / 2) + xy % 2 + yx % 2;
16-
};
17+
return Math.floor(xy / 2) + Math.floor(yx / 2) + (xy % 2) + (yx % 2);
18+
};

solution/2000-2099/2061.Number of Spaces Cleaning Robot Cleaned/README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ class Solution:
9898
return ans
9999
```
100100

101+
```python
102+
class Solution:
103+
def numberOfCleanRooms(self, room: List[List[int]]) -> int:
104+
dirs = (0, 1, 0, -1, 0)
105+
i = j = k = 0
106+
ans = 0
107+
vis = set()
108+
while (i, j, k) not in vis:
109+
vis.add((i, j, k))
110+
ans += room[i][j] == 0
111+
room[i][j] = -1
112+
x, y = i + dirs[k], j + dirs[k + 1]
113+
if 0 <= x < len(room) and 0 <= y < len(room[0]) and room[x][y] != 1:
114+
i, j = x, y
115+
else:
116+
k = (k + 1) % 4
117+
return ans
118+
```
119+
101120
### **Java**
102121

103122
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -133,6 +152,31 @@ class Solution {
133152
}
134153
```
135154

155+
```java
156+
class Solution {
157+
public int numberOfCleanRooms(int[][] room) {
158+
int[] dirs = {0, 1, 0, -1, 0};
159+
int i = 0, j = 0, k = 0;
160+
int m = room.length, n = room[0].length;
161+
boolean[][][] vis = new boolean[m][n][4];
162+
int ans = 0;
163+
while (!vis[i][j][k]) {
164+
vis[i][j][k] = true;
165+
ans += room[i][j] == 0 ? 1 : 0;
166+
room[i][j] = -1;
167+
int x = i + dirs[k], y = j + dirs[k + 1];
168+
if (x >= 0 && x < m && y >= 0 && y < n && room[x][y] != 1) {
169+
i = x;
170+
j = y;
171+
} else {
172+
k = (k + 1) % 4;
173+
}
174+
}
175+
return ans;
176+
}
177+
}
178+
```
179+
136180
### **C++**
137181

138182
```cpp
@@ -164,6 +208,33 @@ public:
164208
};
165209
```
166210
211+
```cpp
212+
class Solution {
213+
public:
214+
int numberOfCleanRooms(vector<vector<int>>& room) {
215+
int dirs[5] = {0, 1, 0, -1, 0};
216+
int i = 0, j = 0, k = 0;
217+
int m = room.size(), n = room[0].size();
218+
bool vis[m][n][4];
219+
memset(vis, false, sizeof(vis));
220+
int ans = 0;
221+
while (!vis[i][j][k]) {
222+
vis[i][j][k] = true;
223+
ans += room[i][j] == 0 ? 1 : 0;
224+
room[i][j] = -1;
225+
int x = i + dirs[k], y = j + dirs[k + 1];
226+
if (x >= 0 && x < m && y >= 0 && y < n && room[x][y] != 1) {
227+
i = x;
228+
j = y;
229+
} else {
230+
k = (k + 1) % 4;
231+
}
232+
}
233+
return ans;
234+
}
235+
};
236+
```
237+
167238
### **Go**
168239

169240
```go
@@ -196,6 +267,32 @@ func numberOfCleanRooms(room [][]int) (ans int) {
196267
}
197268
```
198269

270+
```go
271+
func numberOfCleanRooms(room [][]int) (ans int) {
272+
m, n := len(room), len(room[0])
273+
vis := make([][][4]bool, m)
274+
for i := range vis {
275+
vis[i] = make([][4]bool, n)
276+
}
277+
dirs := [5]int{0, 1, 0, -1, 0}
278+
var i, j, k int
279+
for !vis[i][j][k] {
280+
vis[i][j][k] = true
281+
if room[i][j] == 0 {
282+
ans++
283+
room[i][j] = -1
284+
}
285+
x, y := i+dirs[k], j+dirs[k+1]
286+
if x >= 0 && x < m && y >= 0 && y < n && room[x][y] != 1 {
287+
i, j = x, y
288+
} else {
289+
k = (k + 1) % 4
290+
}
291+
}
292+
return
293+
}
294+
```
295+
199296
### **...**
200297

201298
```

solution/2000-2099/2061.Number of Spaces Cleaning Robot Cleaned/README_EN.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ class Solution:
8282
return ans
8383
```
8484

85+
```python
86+
class Solution:
87+
def numberOfCleanRooms(self, room: List[List[int]]) -> int:
88+
dirs = (0, 1, 0, -1, 0)
89+
i = j = k = 0
90+
ans = 0
91+
vis = set()
92+
while (i, j, k) not in vis:
93+
vis.add((i, j, k))
94+
ans += room[i][j] == 0
95+
room[i][j] = -1
96+
x, y = i + dirs[k], j + dirs[k + 1]
97+
if 0 <= x < len(room) and 0 <= y < len(room[0]) and room[x][y] != 1:
98+
i, j = x, y
99+
else:
100+
k = (k + 1) % 4
101+
return ans
102+
```
103+
85104
### **Java**
86105

87106
```java
@@ -115,6 +134,31 @@ class Solution {
115134
}
116135
```
117136

137+
```java
138+
class Solution {
139+
public int numberOfCleanRooms(int[][] room) {
140+
int[] dirs = {0, 1, 0, -1, 0};
141+
int i = 0, j = 0, k = 0;
142+
int m = room.length, n = room[0].length;
143+
boolean[][][] vis = new boolean[m][n][4];
144+
int ans = 0;
145+
while (!vis[i][j][k]) {
146+
vis[i][j][k] = true;
147+
ans += room[i][j] == 0 ? 1 : 0;
148+
room[i][j] = -1;
149+
int x = i + dirs[k], y = j + dirs[k + 1];
150+
if (x >= 0 && x < m && y >= 0 && y < n && room[x][y] != 1) {
151+
i = x;
152+
j = y;
153+
} else {
154+
k = (k + 1) % 4;
155+
}
156+
}
157+
return ans;
158+
}
159+
}
160+
```
161+
118162
### **C++**
119163

120164
```cpp
@@ -146,6 +190,33 @@ public:
146190
};
147191
```
148192
193+
```cpp
194+
class Solution {
195+
public:
196+
int numberOfCleanRooms(vector<vector<int>>& room) {
197+
int dirs[5] = {0, 1, 0, -1, 0};
198+
int i = 0, j = 0, k = 0;
199+
int m = room.size(), n = room[0].size();
200+
bool vis[m][n][4];
201+
memset(vis, false, sizeof(vis));
202+
int ans = 0;
203+
while (!vis[i][j][k]) {
204+
vis[i][j][k] = true;
205+
ans += room[i][j] == 0 ? 1 : 0;
206+
room[i][j] = -1;
207+
int x = i + dirs[k], y = j + dirs[k + 1];
208+
if (x >= 0 && x < m && y >= 0 && y < n && room[x][y] != 1) {
209+
i = x;
210+
j = y;
211+
} else {
212+
k = (k + 1) % 4;
213+
}
214+
}
215+
return ans;
216+
}
217+
};
218+
```
219+
149220
### **Go**
150221

151222
```go
@@ -178,6 +249,32 @@ func numberOfCleanRooms(room [][]int) (ans int) {
178249
}
179250
```
180251

252+
```go
253+
func numberOfCleanRooms(room [][]int) (ans int) {
254+
m, n := len(room), len(room[0])
255+
vis := make([][][4]bool, m)
256+
for i := range vis {
257+
vis[i] = make([][4]bool, n)
258+
}
259+
dirs := [5]int{0, 1, 0, -1, 0}
260+
var i, j, k int
261+
for !vis[i][j][k] {
262+
vis[i][j][k] = true
263+
if room[i][j] == 0 {
264+
ans++
265+
room[i][j] = -1
266+
}
267+
x, y := i+dirs[k], j+dirs[k+1]
268+
if x >= 0 && x < m && y >= 0 && y < n && room[x][y] != 1 {
269+
i, j = x, y
270+
} else {
271+
k = (k + 1) % 4
272+
}
273+
}
274+
return
275+
}
276+
```
277+
181278
### **...**
182279

183280
```

0 commit comments

Comments
 (0)