모든 챌린지
보통 2명 해결 정답률 100.0%

Tic-Tac-Toe Winner

Decide the result of a tic-tac-toe board.

배우게 될 내용

  • Check rows, columns and diagonals
  • Detect a draw or an unfinished game
  • Keep grid checks readable

문제

번역본 보기

A 3×3 board is given as three strings of X, O and . (empty). A player wins with three of their marks in a row, column or diagonal. Boards never have two winners.

Implement tic_tac_toe_winner(board).

Input

board: Three rows of three characters each.

Output

Return "X" or "O" for the winner, "Draw" if the board is full with no winner, or "Pending" if empty cells remain and nobody has won.

제약 조건

board has exactly 3 rows of length 3

예시

예시 1
입력:
board = ["XOX", ".X.", "O.X"]

예상 출력:
expected = "X"
입력:
std::vector<std::string> board = {"XOX", ".X.", "O.X"};

예상 출력:
std::string expected = "X";
입력:
string[] board = new string[] {"XOX", ".X.", "O.X"};

예상 출력:
string expected = "X";

설명

X holds the diagonal from top-left to bottom-right, so X wins.

예시 2
입력:
board = ["XOX", "XOO", "OXX"]

예상 출력:
expected = "Draw"
입력:
std::vector<std::string> board = {"XOX", "XOO", "OXX"};

예상 출력:
std::string expected = "Draw";
입력:
string[] board = new string[] {"XOX", "XOO", "OXX"};

예상 출력:
string expected = "Draw";

설명

The board is full and no line has three of a kind: Draw.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Check every row, column, and the two diagonals for three identical non-empty marks.

힌트 2

Track whether any empty cells remain while scanning the board once.

힌트 3

Remember that a full board with no winner is a draw, otherwise it is pending.

예상 복잡도

O(1) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 947 ms - 5/5 코드 보기
2 LMLaura Mahlow Python 975 ms - 2/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 2개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.