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

  • 相关阅读:
    数组
    课堂验证性实验总结
    《大道至简》第二章读后感
    大道至简第一章伪代码
    大道至简
    python学习笔记1
    19maven依赖冲突
    18SSM资源整合2
    18SSM资源整合
    17mybatis注解开发
  • 原文地址:https://www.cnblogs.com/Makerr/p/14656349.html
Copyright © 2011-2022 走看看