1812.判断国际象棋棋盘中一个格子的颜色
链接:1812.判断国际象棋棋盘中一个格子的颜色
难度:Easy
标签:数学、字符串
简介:如果所给格子的颜色是白色,请你返回 true,如果是黑色,请返回 false 。
题解 1 - python
- 编辑时间:2024-12-09
- 内存消耗:16.96MB
- 编程语言:python
- 解法介绍:遍历。
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
return bool((ord(coordinates[0]) - ord('a')) % 2 + int(coordinates[1]) % 2 - 1)
题解 2 - cpp
- 编辑时间:2022-12-08
- 内存消耗:5.8MB
- 编程语言:cpp
- 解法介绍:判断行列。
class Solution {
public:
bool squareIsWhite(string coordinates) {
return (coordinates[1] - '0' - 1 ^ ((coordinates[0] - 'a') & 1)) & 1;
}
};
题解 3 - cpp
- 编辑时间:2022-12-08
- 内存消耗:5.8MB
- 编程语言:cpp
- 解法介绍:判断行列。
class Solution {
public:
bool squareIsWhite(string coordinates) {
return ((coordinates[0] - 'a') & 1) == ((coordinates[1] - '0') & 1);
}
};