1812. Determine Color of a Chessboard Square

You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.

The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.

Example 1:

Input: coordinates = "a1"
Output: false
Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.

Constraints:

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

注意找规律,从最简单的思路想起

class Solution {
publicbool squareIsWhite(string c) {
        return (c[0]+c[1])%2;
    }
};
class Solution:
    def squareIsWhite(self, c: str) -> bool:
        return (ord(c[0])+ord(c[1]))%2

需要注意:C++中字符相加是可以的,会自动转成相应的ASCII相加,而Python中需要用ord函数,将字符转换成其对应的ASCII

posted @ 2021-04-14 09:22  Makerr  阅读(43)  评论(0编辑  收藏  举报