LeetCode 1812. 判断国际象棋棋盘中一个格子的颜色

简介: 给你一个坐标 coordinates ,它是一个字符串,表示国际象棋棋盘中一个格子的坐标。下图是国际象棋棋盘示意图。

网络异常,图片无法展示
|


题目


给你一个坐标 coordinates ,它是一个字符串,表示国际象棋棋盘中一个格子的坐标。下图是国际象棋棋盘示意图。


如果所给格子的颜色是白色,请你返回 true,如果是黑色,请返回 false 。


给定坐标一定代表国际象棋棋盘上一个存在的格子。坐标第一个字符是字母,第二个字符是数字。

网络异常,图片无法展示
|

示例 1:
输入:coordinates = "a1"
输出:false
解释:如上图棋盘所示,"a1" 坐标的格子是黑色的,所以返回 false 。
示例 2:
输入:coordinates = "h3"
输出:true
解释:如上图棋盘所示,"h3" 坐标的格子是白色的,所以返回 true 。
示例 3:
输入:coordinates = "c7"
输出:false


提示:


coordinates.length == 2 'a' <= coordinates[0] <= 'h' '1' <= coordinates[1] <= '8'


解题思路


class Solution:
    def squareIsWhite(self, coordinates: str) -> bool:
        # 两个坐标加起来的奇偶性来判断
        one = ord(coordinates[0])-97+1
        two = int(coordinates[1])
        return (one+two)%2 == 1
if __name__ == '__main__':
    coordinates = "a1"
    coordinates = "h3"
    ret = Solution().squareIsWhite(coordinates)
    print(ret)
目录
相关文章
|
1月前
|
存储 算法
LeetCode刷题---75. 颜色分类(双指针,循环不变量)
LeetCode刷题---75. 颜色分类(双指针,循环不变量)
|
3月前
|
Go
golang力扣leetcode 75.颜色分类
golang力扣leetcode 75.颜色分类
22 0
|
2月前
|
Java
|
3月前
leetcode-75:颜色分类
leetcode-75:颜色分类
26 0
LeetCode-2038 如果相邻两个颜色均相同则删除当前颜色
LeetCode-2038 如果相邻两个颜色均相同则删除当前颜色
|
10月前
|
搜索推荐 Java Python
leetcode.75:颜色分类
leetcode.75:颜色分类
38 0
【LeetCode】移动零&&颜色分类&&有序数组的平方&&有效的山脉数组
【LeetCode】移动零&&颜色分类&&有序数组的平方&&有效的山脉数组
【LeetCode】移动零&&颜色分类&&有序数组的平方&&有效的山脉数组
LeetCode 75 Sort Colors 颜色分类(荷兰国旗)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
69 0
|
算法
LeetCode 73矩阵置零&74搜素二维矩阵&75颜色分类
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。
100 0
LeetCode 73矩阵置零&74搜素二维矩阵&75颜色分类