中級 3件解決済み 正答率60.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つずつ開いてみましょう。

ヒント 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 Н"Никита "Programmer" Python 947 ms - 5/5 コードを見る
2 LMLaura Mahlow Python 975 ms - 2/5 コードを見る
3 PKPornchai Kaewkrabil Go 1,357 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が3件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。