[LeetCode] 1812. Determine Color of a Chessboard Square

Chobby·4일 전
1

LeetCode

목록 보기
609/650

😎풀이

  1. 좌표 확인
  2. 문자열 좌표를 숫자로 변환
  3. 두 좌표의 합 계산
    3-1. 홀수라면, 검정색 칸에 가게 됨
    3-2. 짝수라면, 흰 색 칸에 가게 됨
  4. 흰 칸이라면 true, 검정 칸이라면, false 반환환
function squareIsWhite(coordinates: string): boolean {
    const xDict = {
        a: 0,
        b: 1,
        c: 2,
        d: 3,
        e: 4,
        f: 5,
        g: 6,
        h: 7
    }
    const [cx, cy] = [...coordinates]
    const x = xDict[cx]
    const y = Number(cy)
    const isOdd = ((x + y) & 1) === 1
    return !isOdd
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글