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

Lines changed: 5 additions & 9 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 31 additions & 2 deletions
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

Lines changed: 25 additions & 2 deletions
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
```
Lines changed: 8 additions & 0 deletions
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+
};
Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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+
};

0 commit comments

Comments
 (0)