|
6 | 6 |
|
7 | 7 | <p>Tic-tac-toe is played by two players <em>A</em> and <em>B</em> on a <i>3</i> x <i>3</i> grid.</p>
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 | 9 | <p>Here are the rules of Tic-Tac-Toe:</p>
|
12 | 10 |
|
13 |
| - |
14 |
| - |
15 | 11 | <ul>
|
16 | 12 | <li>Players take turns placing characters into empty squares (" ").</li>
|
17 | 13 | <li>The first player <em>A</em> always places "X" characters, while the second player <em>B</em> always places "O" characters.</li>
|
|
21 | 17 | <li>No more moves can be played if the game is over.</li>
|
22 | 18 | </ul>
|
23 | 19 |
|
24 |
| - |
25 |
| - |
26 | 20 | <p>Given an array <code>moves</code> where each element is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which <em>A</em> and <em>B</em> play.</p>
|
27 | 21 |
|
28 |
| - |
29 |
| - |
30 | 22 | <p>Return the winner of the game if it exists (<em>A</em> or <em>B</em>), in case the game ends in a draw return "Draw", if there are still movements to play return "Pending".</p>
|
31 | 23 |
|
32 |
| - |
33 |
| - |
34 | 24 | <p>You can assume that <code>moves</code> is <strong>valid</strong> (It follows the rules of Tic-Tac-Toe), the grid is initially empty and <em>A</em> will play <strong>first</strong>.</p>
|
35 | 25 |
|
36 |
| - |
37 |
| - |
38 | 26 | <p> </p>
|
39 | 27 |
|
40 | 28 | <p><strong>Example 1:</strong></p>
|
41 | 29 |
|
42 |
| - |
43 |
| - |
44 | 30 | <pre>
|
45 | 31 |
|
46 | 32 | <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
|
|
57 | 43 |
|
58 | 44 | </pre>
|
59 | 45 |
|
60 |
| - |
61 |
| - |
62 | 46 | <p><strong>Example 2:</strong></p>
|
63 | 47 |
|
64 |
| - |
65 |
| - |
66 | 48 | <pre>
|
67 | 49 |
|
68 | 50 | <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
|
|
79 | 61 |
|
80 | 62 | </pre>
|
81 | 63 |
|
82 |
| - |
83 |
| - |
84 | 64 | <p><strong>Example 3:</strong></p>
|
85 | 65 |
|
86 |
| - |
87 |
| - |
88 | 66 | <pre>
|
89 | 67 |
|
90 | 68 | <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
|
|
101 | 79 |
|
102 | 80 | </pre>
|
103 | 81 |
|
104 |
| - |
105 |
| - |
106 | 82 | <p><strong>Example 4:</strong></p>
|
107 | 83 |
|
108 |
| - |
109 |
| - |
110 | 84 | <pre>
|
111 | 85 |
|
112 | 86 | <strong>Input:</strong> moves = [[0,0],[1,1]]
|
|
123 | 97 |
|
124 | 98 | </pre>
|
125 | 99 |
|
126 |
| - |
127 |
| - |
128 | 100 | <p> </p>
|
129 | 101 |
|
130 | 102 | <p><strong>Constraints:</strong></p>
|
131 | 103 |
|
132 |
| - |
133 |
| - |
134 | 104 | <ul>
|
135 | 105 | <li><code>1 <= moves.length <= 9</code></li>
|
136 | 106 | <li><code>moves[i].length == 2</code></li>
|
|
146 | 116 | ### **Python3**
|
147 | 117 |
|
148 | 118 | ```python
|
149 |
| - |
| 119 | +class Solution: |
| 120 | + def tictactoe(self, moves: List[List[int]]) -> str: |
| 121 | + n = len(moves) |
| 122 | + counter = [0] * 8 |
| 123 | + for i in range(n - 1, -1, -2): |
| 124 | + row, col = moves[i][0], moves[i][1] |
| 125 | + counter[row] += 1 |
| 126 | + counter[col + 3] += 1 |
| 127 | + if row == col: |
| 128 | + counter[6] += 1 |
| 129 | + if row + col == 2: |
| 130 | + counter[7] += 1 |
| 131 | + if counter[row] == 3 or counter[col + 3] == 3 or counter[6] == 3 or counter[7] == 3: |
| 132 | + return "A" if (i % 2) == 0 else "B" |
| 133 | + return "Draw" if n == 9 else "Pending" |
150 | 134 | ```
|
151 | 135 |
|
152 | 136 | ### **Java**
|
153 | 137 |
|
154 | 138 | ```java
|
| 139 | +class Solution { |
| 140 | + public String tictactoe(int[][] moves) { |
| 141 | + int n = moves.length; |
| 142 | + int[] counter = new int[8]; |
| 143 | + for (int i = n - 1; i >= 0; i -= 2) { |
| 144 | + int row = moves[i][0], col = moves[i][1]; |
| 145 | + ++counter[row]; |
| 146 | + ++counter[col + 3]; |
| 147 | + if (row == col) ++counter[6]; |
| 148 | + if (row + col == 2) ++counter[7]; |
| 149 | + if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) { |
| 150 | + return (i % 2) == 0 ? "A" : "B"; |
| 151 | + } |
| 152 | + } |
| 153 | + return n == 9 ? "Draw" : "Pending"; |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
155 | 157 |
|
| 158 | +### **C++** |
| 159 | + |
| 160 | +```cpp |
| 161 | +class Solution { |
| 162 | +public: |
| 163 | + string tictactoe(vector<vector<int>>& moves) { |
| 164 | + int n = moves.size(); |
| 165 | + vector<int> counter(8, 0); |
| 166 | + for (int i = n - 1; i >= 0; i -= 2) { |
| 167 | + int row = moves[i][0], col = moves[i][1]; |
| 168 | + ++counter[row]; |
| 169 | + ++counter[col + 3]; |
| 170 | + if (row == col) ++counter[6]; |
| 171 | + if (row + col == 2) ++counter[7]; |
| 172 | + if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) { |
| 173 | + return (i % 2 == 0) ? "A" : "B"; |
| 174 | + } |
| 175 | + } |
| 176 | + return n == 9 ? "Draw" : "Pending"; |
| 177 | + } |
| 178 | +}; |
156 | 179 | ```
|
157 | 180 |
|
158 | 181 | ### **...**
|
|
0 commit comments