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

This commit is contained in:
huangge1199 2021-06-07 11:30:03 +08:00
parent ef1a063b41
commit 4cec7b7073
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,67 @@
//给你一个坐标 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'
//
// Related Topics 字符串
// 👍 4 👎 0
package leetcode.editor.cn;
//1812:判断国际象棋棋盘中一个格子的颜色
public class DetermineColorOfAChessboardSquare{
public static void main(String[] args) {
//测试代码
Solution solution = new DetermineColorOfAChessboardSquare().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean squareIsWhite(String coordinates) {
char[] chars = coordinates.toCharArray();
if ((chars[0] - chars[1]) % 2 == 0) {
return false;
} else {
return true;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,43 @@
<p>给你一个坐标 <code>coordinates</code> ,它是一个字符串,表示国际象棋棋盘中一个格子的坐标。下图是国际象棋棋盘示意图。</p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/03/chessboard.png" style="width: 400px; height: 396px;" /></p>
<p>如果所给格子的颜色是白色,请你返回 <code>true</code>,如果是黑色,请返回 <code>false</code> 。</p>
<p>给定坐标一定代表国际象棋棋盘上一个存在的格子。坐标第一个字符是字母,第二个字符是数字。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>coordinates = "a1"
<b>输出:</b>false
<b>解释:</b>如上图棋盘所示,"a1" 坐标的格子是黑色的,所以返回 false 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>coordinates = "h3"
<b>输出:</b>true
<b>解释:</b>如上图棋盘所示,"h3" 坐标的格子是白色的,所以返回 true 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>coordinates = "c7"
<b>输出:</b>false
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>coordinates.length == 2</code></li>
<li><code>'a' <= coordinates[0] <= 'h'</code></li>
<li><code>'1' <= coordinates[1] <= '8'</code></li>
</ul>
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 4</li><li>👎 0</li></div>