Skip to content

Commit 6b90eb9

Browse files
committed
feat: update solutions to lc problem: No.1812
No.1812.Determine Color of a Chessboard Square
1 parent e391b99 commit 6b90eb9

File tree

7 files changed

+25
-47
lines changed

7 files changed

+25
-47
lines changed

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

+11-17
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@
5353

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

56-
**方法一:奇偶判断**
56+
**方法一:找规律**
5757

58-
根据 `coordinates` 获取对应的坐标 $(x, y)$,如果 $(x + y)$ 为奇数,则格子为白色,返回 `true`,否则返回 `false`
58+
观察棋盘我们发现,颜色相同的两个格子 $(x_1, y_1)$ 和 $(x_2, y_2)$ 满足 $x_1 + y_1$ 和 $x_2 + y_2$ 均为奇数或偶数。
59+
60+
因此,我们可以根据 `coordinates` 获取对应的坐标 $(x, y)$,如果 $x + y$ 为奇数,则格子为白色,返回 `true`,否则返回 `false`
5961

6062
时间复杂度 $O(1)$。
6163

@@ -68,9 +70,7 @@
6870
```python
6971
class Solution:
7072
def squareIsWhite(self, coordinates: str) -> bool:
71-
x = ord(coordinates[0]) - ord('a') + 1
72-
y = int(coordinates[1])
73-
return ((x + y) & 1) == 1
73+
return (ord(coordinates[0]) + ord(coordinates[1])) % 2 == 1
7474
```
7575

7676
### **Java**
@@ -80,9 +80,7 @@ class Solution:
8080
```java
8181
class Solution {
8282
public boolean squareIsWhite(String coordinates) {
83-
int x = coordinates.charAt(0) - 'a' + 1;
84-
int y = coordinates.charAt(1) - '0';
85-
return ((x + y) & 1) == 1;
83+
return (coordinates.charAt(0) + coordinates.charAt(1)) % 2 == 1;
8684
}
8785
}
8886
```
@@ -93,9 +91,7 @@ class Solution {
9391
class Solution {
9492
public:
9593
bool squareIsWhite(string coordinates) {
96-
int x = coordinates[0] - 'a' + 1;
97-
int y = coordinates[1] - '0';
98-
return ((x + y) & 1) == 1;
94+
return (coordinates[0] + coordinates[1]) % 2;
9995
}
10096
};
10197
```
@@ -104,9 +100,7 @@ public:
104100
105101
```go
106102
func squareIsWhite(coordinates string) bool {
107-
x := coordinates[0] - 'a' + 1
108-
y := coordinates[1] - '0'
109-
return ((x + y) & 1) == 1
103+
return (coordinates[0]+coordinates[1])%2 == 1
110104
}
111105
```
112106

@@ -118,9 +112,9 @@ func squareIsWhite(coordinates string) bool {
118112
* @return {boolean}
119113
*/
120114
var squareIsWhite = function (coordinates) {
121-
const x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
122-
const y = Number(coordinates.charAt(1));
123-
return ((x + y) & 1) == 1;
115+
const x = coordinates.charAt(0).charCodeAt();
116+
const y = coordinates.charAt(1).charCodeAt();
117+
return (x + y) % 2 == 1;
124118
};
125119
```
126120

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

+7-15
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,15 @@
5454
```python
5555
class Solution:
5656
def squareIsWhite(self, coordinates: str) -> bool:
57-
x = ord(coordinates[0]) - ord('a') + 1
58-
y = int(coordinates[1])
59-
return ((x + y) & 1) == 1
57+
return (ord(coordinates[0]) + ord(coordinates[1])) % 2 == 1
6058
```
6159

6260
### **Java**
6361

6462
```java
6563
class Solution {
6664
public boolean squareIsWhite(String coordinates) {
67-
int x = coordinates.charAt(0) - 'a' + 1;
68-
int y = coordinates.charAt(1) - '0';
69-
return ((x + y) & 1) == 1;
65+
return (coordinates.charAt(0) + coordinates.charAt(1)) % 2 == 1;
7066
}
7167
}
7268
```
@@ -77,9 +73,7 @@ class Solution {
7773
class Solution {
7874
public:
7975
bool squareIsWhite(string coordinates) {
80-
int x = coordinates[0] - 'a' + 1;
81-
int y = coordinates[1] - '0';
82-
return ((x + y) & 1) == 1;
76+
return (coordinates[0] + coordinates[1]) % 2;
8377
}
8478
};
8579
```
@@ -88,9 +82,7 @@ public:
8882
8983
```go
9084
func squareIsWhite(coordinates string) bool {
91-
x := coordinates[0] - 'a' + 1
92-
y := coordinates[1] - '0'
93-
return ((x + y) & 1) == 1
85+
return (coordinates[0]+coordinates[1])%2 == 1
9486
}
9587
```
9688

@@ -102,9 +94,9 @@ func squareIsWhite(coordinates string) bool {
10294
* @return {boolean}
10395
*/
10496
var squareIsWhite = function (coordinates) {
105-
const x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
106-
const y = Number(coordinates.charAt(1));
107-
return ((x + y) & 1) == 1;
97+
const x = coordinates.charAt(0).charCodeAt();
98+
const y = coordinates.charAt(1).charCodeAt();
99+
return (x + y) % 2 == 1;
108100
};
109101
```
110102

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Solution {
22
public:
33
bool squareIsWhite(string coordinates) {
4-
int x = coordinates[0] - 'a' + 1;
5-
int y = coordinates[1] - '0';
6-
return ((x + y) & 1) == 1;
4+
return (coordinates[0] + coordinates[1]) % 2;
75
}
86
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
func squareIsWhite(coordinates string) bool {
2-
x := coordinates[0] - 'a' + 1
3-
y := coordinates[1] - '0'
4-
return ((x + y) & 1) == 1
2+
return (coordinates[0]+coordinates[1])%2 == 1
53
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class Solution {
22
public boolean squareIsWhite(String coordinates) {
3-
int x = coordinates.charAt(0) - 'a' + 1;
4-
int y = coordinates.charAt(1) - '0';
5-
return ((x + y) & 1) == 1;
3+
return (coordinates.charAt(0) + coordinates.charAt(1)) % 2 == 1;
64
}
75
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @return {boolean}
44
*/
55
var squareIsWhite = function (coordinates) {
6-
const x = coordinates.charAt(0).charCodeAt() - 'a'.charCodeAt() + 1;
7-
const y = Number(coordinates.charAt(1));
8-
return ((x + y) & 1) == 1;
6+
const x = coordinates.charAt(0).charCodeAt();
7+
const y = coordinates.charAt(1).charCodeAt();
8+
return (x + y) % 2 == 1;
99
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
class Solution:
22
def squareIsWhite(self, coordinates: str) -> bool:
3-
x = ord(coordinates[0]) - ord('a') + 1
4-
y = int(coordinates[1])
5-
return ((x + y) & 1) == 1
3+
return (ord(coordinates[0]) + ord(coordinates[1])) % 2 == 1

0 commit comments

Comments
 (0)