-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1812-DetermineColorOfAChessboardSquare.go
More file actions
64 lines (52 loc) · 2.06 KB
/
1812-DetermineColorOfAChessboardSquare.go
File metadata and controls
64 lines (52 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
// 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.
// <img src="https://assets.leetcode.com/uploads/2021/02/19/screenshot-2021-02-20-at-22159-pm.png" />
// 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.
// Example 2:
// Input: coordinates = "h3"
// Output: true
// Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.
// Example 3:
// Input: coordinates = "c7"
// Output: false
// Constraints:
// coordinates.length == 2
// 'a' <= coordinates[0] <= 'h'
// '1' <= coordinates[1] <= '8'
import "fmt"
func squareIsWhite(coordinates string) bool {
if int(coordinates[0] - 'a') % 2 == 0 { // 偶数列 奇数为 白 ( 0 开头)
return int(coordinates[1] - '0') % 2 == 0
}
return int(coordinates[1] - '0') % 2 == 1
}
func squareIsWhite1(coordinates string) bool {
return (coordinates[0] ^ coordinates[1]) & 1 == 1
}
func main() {
// Example 1:
// Input: coordinates = "a1"
// Output: false
// Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.
fmt.Println(squareIsWhite("a1")) // false
// Example 2:
// Input: coordinates = "h3"
// Output: true
// Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.
fmt.Println(squareIsWhite("h3")) // true
// Example 3:
// Input: coordinates = "c7"
// Output: false
fmt.Println(squareIsWhite("c7")) // false
fmt.Println(squareIsWhite1("a1")) // false
fmt.Println(squareIsWhite1("h3")) // true
fmt.Println(squareIsWhite1("c7")) // false
}