zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    二维数组最大关联子数组
    四则运算(终极版)
    最大子数组
    四则运算(三) 记录日志
    四则运算(三)
    四则运算记录日志
    四则运算(二)
    简单web四则运算出题
    Daily Scrum
    Daily Scrum
  • 原文地址:https://www.cnblogs.com/Makerr/p/14656349.html
Copyright © 2011-2022 走看看