알고리즘/LeetCode
LeetCode 풀기 - 1812. Determine Color of a Chessboard Square
앤테바
2021. 11. 21. 13:21
반응형
1812. Determine Color of a Chessboard Square
https://leetcode.com/problems/determine-color-of-a-chessboard-square/
Determine Color of a Chessboard Square - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1)
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
# black -> False
# white = True
d = {
'a': 1,
'b': 0,
'c': 1,
'd': 0,
'e': 1,
'f': 0,
'g': 1,
'h': 0,
}
if d[coordinates[0]] == 1 and int(coordinates[1]) % 2 == 1:
return False
elif d[coordinates[0]] == 0 and int(coordinates[1]) % 2 == 0:
return False
return True
솔루션2)
솔루션1)d의 hash 자료구조를 문자를 ord()를 이용해서 ascii number로 변환하는 방법으로 대체할 수 있습니다.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
return ord(coordinates[0]) % 2 != int(coordinates[1]) % 2
솔루션3)
굉장히 직관적인 솔루션입니다.
이보 다 더 심플할 순 없다...
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
if coordinates[0] in 'aceg':
return coordinates[1] in '2468'
else:
return coordinates[1] in '1357'
반응형