LeetCode 794. Valid Tic-Tac-Toe State

原题链接在这里:https://leetcode.com/problems/valid-tic-tac-toe-state/

题目:

A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.

The board is a 3 x 3 array, and consists of characters " ""X", and "O".  The " " character represents an empty square.

Here are the rules of Tic-Tac-Toe:

  • Players take turns placing characters into empty squares (" ").
  • The first player always places "X" characters, while the second player always places "O" characters.
  • "X" and "O" characters are always placed into empty squares, never filled ones.
  • The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
  • The game also ends if all squares are non-empty.
  • No more moves can be played if the game is over.
Example 1:
Input: board = ["O  ", "   ", "   "]
Output: false
Explanation: The first player always plays "X".

Example 2:
Input: board = ["XOX", " X ", "   "]
Output: false
Explanation: Players take turns making moves.

Example 3:
Input: board = ["XXX", "   ", "OOO"]
Output: false

Example 4:
Input: board = ["XOX", "O O", "XOX"]
Output: true

Note:

  • board is a length-3 array of strings, where each string board[i] has length 3.
  • Each board[i][j] is a character in the set {" ", "X", "O"}.

题解:

X player goes first. turn++.

O player goes next. turn--.

When checking the board, check row, col and diagonal, to see if there is n or -n. If yes, then x wins with n, o wins with -n.

When x wins turn must be 1. When o wins turn must be 0. If not, return false.

Check if turn is within 0 and 1.

Time Complexity: O(n ^ 2). n = board.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public boolean validTicTacToe(String[] board) {
 3         int n = board.length;
 4         int turn = 0;
 5         boolean xWin = false;
 6         boolean oWin = false;
 7         int [] row = new int[n];
 8         int [] col = new int[n];
 9         int d = 0;
10         int antiD = 0;
11         
12         for(int i = 0; i < n; i++){
13             for(int j = 0; j < n; j++){
14                 if(board[i].charAt(j) == 'X'){
15                     turn++;
16                     row[i]++;
17                     col[j]++;
18                     if(i == j){
19                         d++;
20                     }
21                     
22                     if(i + j == 2){
23                         antiD++;
24                     }
25                 }else if(board[i].charAt(j) == 'O'){
26                     turn--;
27                     row[i]--;
28                     col[j]--;
29                     if(i == j){
30                         d--;
31                     }
32                     
33                     if(i + j == 2){
34                         antiD--;
35                     }
36                 }
37             }
38         }
39         
40         for(int i = 0; i < n; i++){
41             xWin |= row[i] == n;
42             xWin |= col[i] == n;
43             
44             oWin |= row[i] == -n;
45             oWin |= col[i] == -n;
46         }
47         
48         xWin = xWin || d == n || antiD == n;
49         oWin = oWin || d == -n || antiD == -n;
50         
51         if((xWin && turn != 1) || (oWin && turn != 0)){
52             return false;
53         }
54         
55         return turn == 0 || turn == 1;
56     }
57 }

类似Design Tic-Tac-Toe.

posted @ 2019-12-27 09:25  Dylan_Java_NYC  阅读(264)  评论(0编辑  收藏  举报