Skip to content

Commit 46e6dcf

Browse files
committed
feat: update solutions to lc problems
* No.1812.Determine Color of a Chessboard Square * No.2491.Divide Players Into Teams of Equal Skill * No.2493.Divide Nodes Into the Maximum Number of Groups
1 parent c34bf88 commit 46e6dcf

File tree

15 files changed

+111
-53
lines changed

15 files changed

+111
-53
lines changed

solution/1800-1899/1805.Number of Different Integers in a String/README.md

+5-9
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454

5555
**方法一:双指针 + 模拟**
5656

57-
遍历字符串,找到每个整数的起始位置和结束位置,截取出这一个子串,将其存入哈希表 `s` 中。
57+
遍历字符串 `word`,找到每个整数的起始位置和结束位置,截取出这一个子串,将其存入哈希表 $s$ 中。
5858

59-
最后返回哈希表 `s` 的大小即可。
59+
遍历结束,返回哈希表 $s$ 的大小即可。
6060

61-
注意,这里要去掉整数的前导零,并且不能直接将字符串转为整数,因为整数可能很大,超出 `int` 的范围
61+
> 注意,每个子串所表示的整数可能很大,我们不能直接将其转为整数。因此,我们可以去掉每个子串的前导零之后,再存入哈希表
6262
6363
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `word` 的长度。
6464

@@ -123,13 +123,9 @@ public:
123123
int n = word.size();
124124
for (int i = 0; i < n; ++i) {
125125
if (isdigit(word[i])) {
126-
while (i < n && word[i] == '0') {
127-
++i;
128-
}
126+
while (i < n && word[i] == '0') ++i;
129127
int j = i;
130-
while (j < n && isdigit(word[j])) {
131-
++j;
132-
}
128+
while (j < n && isdigit(word[j])) ++j;
133129
s.insert(word.substr(i, j - i));
134130
i = j;
135131
}

solution/1800-1899/1805.Number of Different Integers in a String/README_EN.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,9 @@ public:
104104
int n = word.size();
105105
for (int i = 0; i < n; ++i) {
106106
if (isdigit(word[i])) {
107-
while (i < n && word[i] == '0') {
108-
++i;
109-
}
107+
while (i < n && word[i] == '0') ++i;
110108
int j = i;
111-
while (j < n && isdigit(word[j])) {
112-
++j;
113-
}
109+
while (j < n && isdigit(word[j])) ++j;
114110
s.insert(word.substr(i, j - i));
115111
i = j;
116112
}

solution/1800-1899/1805.Number of Different Integers in a String/Solution.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ class Solution {
55
int n = word.size();
66
for (int i = 0; i < n; ++i) {
77
if (isdigit(word[i])) {
8-
while (i < n && word[i] == '0') {
9-
++i;
10-
}
8+
while (i < n && word[i] == '0') ++i;
119
int j = i;
12-
while (j < n && isdigit(word[j])) {
13-
++j;
14-
}
10+
while (j < n && isdigit(word[j])) ++j;
1511
s.insert(word.substr(i, j - i));
1612
i = j;
1713
}

solution/1800-1899/1812.Determine Color of a Chessboard Square/README.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

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

56+
**方法一:奇偶判断**
57+
58+
根据 `coordinates` 获取对应的坐标 $(x, y)$,如果 $(x + y)$ 为奇数,则格子为白色,返回 `true`,否则返回 `false`
59+
60+
时间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
@@ -81,6 +87,29 @@ class Solution {
8187
}
8288
```
8389

90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
bool squareIsWhite(string coordinates) {
96+
int x = coordinates[0] - 'a' + 1;
97+
int y = coordinates[1] - '0';
98+
return ((x + y) & 1) == 1;
99+
}
100+
};
101+
```
102+
103+
### **Go**
104+
105+
```go
106+
func squareIsWhite(coordinates string) bool {
107+
x := coordinates[0] - 'a' + 1
108+
y := coordinates[1] - '0'
109+
return ((x + y) & 1) == 1
110+
}
111+
```
112+
84113
### **JavaScript**
85114

86115
```js
@@ -89,8 +118,8 @@ class Solution {
89118
* @return {boolean}
90119
*/
91120
var squareIsWhite = function (coordinates) {
92-
let x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
93-
let y = Number(coordinates.charAt(1));
121+
const x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
122+
const y = Number(coordinates.charAt(1));
94123
return ((x + y) & 1) == 1;
95124
};
96125
```

solution/1800-1899/1812.Determine Color of a Chessboard Square/README_EN.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ class Solution {
7171
}
7272
```
7373

74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
bool squareIsWhite(string coordinates) {
80+
int x = coordinates[0] - 'a' + 1;
81+
int y = coordinates[1] - '0';
82+
return ((x + y) & 1) == 1;
83+
}
84+
};
85+
```
86+
87+
### **Go**
88+
89+
```go
90+
func squareIsWhite(coordinates string) bool {
91+
x := coordinates[0] - 'a' + 1
92+
y := coordinates[1] - '0'
93+
return ((x + y) & 1) == 1
94+
}
95+
```
96+
7497
### **JavaScript**
7598

7699
```js
@@ -79,8 +102,8 @@ class Solution {
79102
* @return {boolean}
80103
*/
81104
var squareIsWhite = function (coordinates) {
82-
let x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
83-
let y = Number(coordinates.charAt(1));
105+
const x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
106+
const y = Number(coordinates.charAt(1));
84107
return ((x + y) & 1) == 1;
85108
};
86109
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
bool squareIsWhite(string coordinates) {
4+
int x = coordinates[0] - 'a' + 1;
5+
int y = coordinates[1] - '0';
6+
return ((x + y) & 1) == 1;
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func squareIsWhite(coordinates string) bool {
2+
x := coordinates[0] - 'a' + 1
3+
y := coordinates[1] - '0'
4+
return ((x + y) & 1) == 1
5+
}

solution/1800-1899/1812.Determine Color of a Chessboard Square/Solution.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @return {boolean}
44
*/
55
var squareIsWhite = function (coordinates) {
6-
let x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
7-
let y = Number(coordinates.charAt(1));
6+
const x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
7+
const y = Number(coordinates.charAt(1));
88
return ((x + y) & 1) == 1;
99
};

solution/2400-2499/2490.Circular Sentence/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ impl Solution {
207207
}
208208
```
209209

210-
211210
### **...**
212211

213212
```

solution/2400-2499/2490.Circular Sentence/Solution.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var isCircularSentence = function(sentence) {
1+
var isCircularSentence = function (sentence) {
22
const words = sentence.split(' ');
33
const post = words[0].charCodeAt(0);
44
let prev = words[0].charCodeAt(words[0].length - 1);
@@ -11,4 +11,4 @@ var isCircularSentence = function(sentence) {
1111
prev = cur.charCodeAt(cur.length - 1);
1212
}
1313
return post === prev;
14-
};
14+
};

solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,15 @@ func dividePlayers(skill []int) int64 {
227227
### **JavaScript**
228228

229229
```js
230-
var dividePlayers = function(skill) {
231-
const n = skill.length, m = n / 2;
230+
var dividePlayers = function (skill) {
231+
const n = skill.length,
232+
m = n / 2;
232233
skill.sort((a, b) => a - b);
233234
const sum = skill[0] + skill[n - 1];
234235
let ans = 0;
235236
for (let i = 0; i < m; i++) {
236-
const x = skill[i], y = skill[n - 1 - i];
237+
const x = skill[i],
238+
y = skill[n - 1 - i];
237239
if (x + y != sum) return -1;
238240
ans += x * y;
239241
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
var dividePlayers = function(skill) {
2-
const n = skill.length, m = n / 2;
1+
var dividePlayers = function (skill) {
2+
const n = skill.length,
3+
m = n / 2;
34
skill.sort((a, b) => a - b);
45
const sum = skill[0] + skill[n - 1];
56
let ans = 0;
67
for (let i = 0; i < m; i++) {
7-
const x = skill[i], y = skill[n - 1 - i];
8+
const x = skill[i],
9+
y = skill[n - 1 - i];
810
if (x + y != sum) return -1;
911
ans += x * y;
1012
}
1113
return ans;
12-
};
14+
};

solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ func abs(x int) int {
369369
### **JavaScript**
370370

371371
```js
372-
var magnificentSets = function(n, edges) {
373-
const graph = Array.from({length: n + 1}, () => new Set());
372+
var magnificentSets = function (n, edges) {
373+
const graph = Array.from({ length: n + 1 }, () => new Set());
374374
for (const [u, v] of edges) {
375375
graph[u].add(v);
376376
graph[v].add(u);
@@ -382,7 +382,8 @@ var magnificentSets = function(n, edges) {
382382
let queue = [i];
383383
const dis = Array(n + 1).fill(0);
384384
dis[i] = 1;
385-
let mx = 1, mn = n;
385+
let mx = 1,
386+
mn = n;
386387
while (queue.length) {
387388
let next = [];
388389
for (let u of queue) {
@@ -400,7 +401,7 @@ var magnificentSets = function(n, edges) {
400401
}
401402
queue = next;
402403
}
403-
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
404+
hash.set(mn, Math.max(mx, hash.get(mn) || 0));
404405
}
405406

406407
let ans = 0;

solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Solution {
115115
private List<Integer> arr = new ArrayList<>();
116116
private boolean[] vis;
117117
private int n;
118-
118+
119119
public int magnificentSets(int n, int[][] edges) {
120120
g = new List[n + 1];
121121
this.n = n;
@@ -125,7 +125,7 @@ class Solution {
125125
g[a].add(b);
126126
g[b].add(a);
127127
}
128-
128+
129129
vis = new boolean[n + 1];
130130
int ans = 0;
131131
for (int i = 1; i <= n; ++i) {
@@ -351,8 +351,8 @@ func abs(x int) int {
351351
### **JavaScript**
352352

353353
```js
354-
var magnificentSets = function(n, edges) {
355-
const graph = Array.from({length: n + 1}, () => new Set());
354+
var magnificentSets = function (n, edges) {
355+
const graph = Array.from({ length: n + 1 }, () => new Set());
356356
for (const [u, v] of edges) {
357357
graph[u].add(v);
358358
graph[v].add(u);
@@ -364,7 +364,8 @@ var magnificentSets = function(n, edges) {
364364
let queue = [i];
365365
const dis = Array(n + 1).fill(0);
366366
dis[i] = 1;
367-
let mx = 1, mn = n;
367+
let mx = 1,
368+
mn = n;
368369
while (queue.length) {
369370
let next = [];
370371
for (let u of queue) {
@@ -382,7 +383,7 @@ var magnificentSets = function(n, edges) {
382383
}
383384
queue = next;
384385
}
385-
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
386+
hash.set(mn, Math.max(mx, hash.get(mn) || 0));
386387
}
387388

388389
let ans = 0;
@@ -393,7 +394,6 @@ var magnificentSets = function(n, edges) {
393394
};
394395
```
395396

396-
397397
### **...**
398398

399399
```

solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/Solution.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var magnificentSets = function(n, edges) {
2-
const graph = Array.from({length: n + 1}, () => new Set());
1+
var magnificentSets = function (n, edges) {
2+
const graph = Array.from({ length: n + 1 }, () => new Set());
33
for (const [u, v] of edges) {
44
graph[u].add(v);
55
graph[v].add(u);
@@ -11,7 +11,8 @@ var magnificentSets = function(n, edges) {
1111
let queue = [i];
1212
const dis = Array(n + 1).fill(0);
1313
dis[i] = 1;
14-
let mx = 1, mn = n;
14+
let mx = 1,
15+
mn = n;
1516
while (queue.length) {
1617
let next = [];
1718
for (let u of queue) {
@@ -29,12 +30,12 @@ var magnificentSets = function(n, edges) {
2930
}
3031
queue = next;
3132
}
32-
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
33+
hash.set(mn, Math.max(mx, hash.get(mn) || 0));
3334
}
3435

3536
let ans = 0;
3637
for (const [u, v] of hash) {
3738
ans += v;
3839
}
3940
return ans;
40-
};
41+
};

0 commit comments

Comments
 (0)