Tous les défis
Moyen 3 résolus 60.0 % de réussite

Tic-Tac-Toe Winner

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

board has exactly 3 rows of length 3

Exemples

Exemple 1
Entrée :
board = ["XOX", ".X.", "O.X"]

Sortie attendue :
expected = "X"
Entrée :
std::vector<std::string> board = {"XOX", ".X.", "O.X"};

Sortie attendue :
std::string expected = "X";
Entrée :
string[] board = new string[] {"XOX", ".X.", "O.X"};

Sortie attendue :
string expected = "X";

Explication

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

Exemple 2
Entrée :
board = ["XOX", "XOO", "OXX"]

Sortie attendue :
expected = "Draw"
Entrée :
std::vector<std::string> board = {"XOX", "XOO", "OXX"};

Sortie attendue :
std::string expected = "Draw";
Entrée :
string[] board = new string[] {"XOX", "XOO", "OXX"};

Sortie attendue :
string expected = "Draw";

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 Н"Никита "Programmer" Python 947 ms - 5/5 Voir le code
2 LMLaura Mahlow Python 975 ms - 2/5 Voir le code
3 PKPornchai Kaewkrabil Go 1,357 ms - 4/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 3 solutions complètes ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.