Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0fe1647

Browse files
feat: add solutions to lc problem: No.0794
No.0794.Valid Tic-Tac-Toe State
1 parent 711b878 commit 0fe1647

File tree

6 files changed

+423
-2
lines changed

6 files changed

+423
-2
lines changed

‎solution/0700-0799/0794.Valid Tic-Tac-Toe State/README.md‎

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,158 @@
6060
<!-- 这里可写当前语言的特殊实现逻辑 -->
6161

6262
```python
63-
63+
class Solution:
64+
def validTicTacToe(self, board: List[str]) -> bool:
65+
66+
def win(p):
67+
for i in range(3):
68+
if board[i][0] == board[i][1] == board[i][2] == p:
69+
return True
70+
if board[0][i] == board[1][i] == board[2][i] == p:
71+
return True
72+
if board[0][0] == board[1][1] == board[2][2] == p:
73+
return True
74+
return board[0][2] == board[1][1] == board[2][0] == p
75+
76+
x, o = 0, 0
77+
for i in range(3):
78+
for j in range(3):
79+
if board[i][j] == 'X':
80+
x += 1
81+
elif board[i][j] == 'O':
82+
o += 1
83+
84+
if x != o and x - 1 != o:
85+
return False
86+
87+
if win('X') and x - 1 != o:
88+
return False
89+
90+
return not (win('O') and x != o)
6491
```
6592

6693
### **Java**
6794

6895
<!-- 这里可写当前语言的特殊实现逻辑 -->
6996

7097
```java
98+
class Solution {
99+
public boolean validTicTacToe(String[] board) {
100+
int x = 0, o = 0;
101+
for (int i = 0; i < 3; i++) {
102+
for (int j = 0; j < 3; j++) {
103+
if (board[i].charAt(j) == 'X') {
104+
x++;
105+
} else if (board[i].charAt(j) == 'O') {
106+
o++;
107+
}
108+
}
109+
}
110+
if (x != o && x - 1 != o) {
111+
return false;
112+
}
113+
if (win(board, 'X') && x - 1 != o) {
114+
return false;
115+
}
116+
return !(win(board, 'O') && x != o);
117+
}
118+
119+
private boolean win(String[] b, char p) {
120+
for (int i = 0; i < 3; i++) {
121+
if (b[i].charAt(0) == p && b[i].charAt(1) == p && b[i].charAt(2) == p) {
122+
return true;
123+
}
124+
if (b[0].charAt(i) == p && b[1].charAt(i) == p && b[2].charAt(i) == p) {
125+
return true;
126+
}
127+
}
128+
if (b[0].charAt(0) == p && b[1].charAt(1) == p && b[2].charAt(2) == p) {
129+
return true;
130+
}
131+
return b[0].charAt(2) == p && b[1].charAt(1) == p && b[2].charAt(0) == p;
132+
}
133+
}
134+
```
135+
136+
### **Go**
137+
138+
```go
139+
func validTicTacToe(board []string) bool {
140+
x, o := 0, 0
141+
for i := 0; i < 3; i++ {
142+
for j := 0; j < 3; j++ {
143+
if board[i][j] == 'X' {
144+
x++
145+
} else if board[i][j] == 'O' {
146+
o++
147+
}
148+
}
149+
}
150+
if x != o && x-1 != o {
151+
return false
152+
}
153+
if win(board, 'X') && x-1 != o {
154+
return false
155+
}
156+
return !(win(board, 'O') && x != o)
157+
}
158+
159+
func win(b []string, p byte) bool {
160+
for i := 0; i < 3; i++ {
161+
if b[i][0] == p && b[i][1] == p && b[i][2] == p {
162+
return true
163+
}
164+
if b[0][i] == p && b[1][i] == p && b[2][i] == p {
165+
return true
166+
}
167+
}
168+
if b[0][0] == p && b[1][1] == p && b[2][2] == p {
169+
return true
170+
}
171+
return b[0][2] == p && b[1][1] == p && b[2][0] == p
172+
}
173+
```
71174

175+
### **C++**
176+
177+
```cpp
178+
class Solution {
179+
public:
180+
bool validTicTacToe(vector<string>& board) {
181+
int x = 0, o = 0;
182+
for (int i = 0; i < 3; ++i) {
183+
for (int j = 0; j < 3; ++j) {
184+
if (board[i][j] == 'X') {
185+
++x;
186+
} else if (board[i][j] == 'O') {
187+
++o;
188+
}
189+
}
190+
}
191+
if (x != o && x - 1 != o) {
192+
return false;
193+
}
194+
if (win(board, 'X') && x - 1 != o) {
195+
return false;
196+
}
197+
return !(win(board, 'O') && x != o);
198+
}
199+
200+
bool win(vector<string>& b, char p) {
201+
for (int i = 0; i < 3; ++i) {
202+
if (b[i][0] == p && b[i][1] == p && b[i][2] == p) {
203+
return true;
204+
}
205+
if (b[0][i] == p && b[1][i] == p && b[2][i] == p) {
206+
return true;
207+
}
208+
}
209+
if (b[0][0] == p && b[1][1] == p && b[2][2] == p) {
210+
return true;
211+
}
212+
return b[0][2] == p && b[1][1] == p && b[2][0] == p;
213+
}
214+
};
72215
```
73216

74217
### **...**

‎solution/0700-0799/0794.Valid Tic-Tac-Toe State/README_EN.md‎

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,156 @@
8585
### **Python3**
8686

8787
```python
88-
88+
class Solution:
89+
def validTicTacToe(self, board: List[str]) -> bool:
90+
91+
def win(p):
92+
for i in range(3):
93+
if board[i][0] == board[i][1] == board[i][2] == p:
94+
return True
95+
if board[0][i] == board[1][i] == board[2][i] == p:
96+
return True
97+
if board[0][0] == board[1][1] == board[2][2] == p:
98+
return True
99+
return board[0][2] == board[1][1] == board[2][0] == p
100+
101+
x, o = 0, 0
102+
for i in range(3):
103+
for j in range(3):
104+
if board[i][j] == 'X':
105+
x += 1
106+
elif board[i][j] == 'O':
107+
o += 1
108+
109+
if x != o and x - 1 != o:
110+
return False
111+
112+
if win('X') and x - 1 != o:
113+
return False
114+
115+
return not (win('O') and x != o)
89116
```
90117

91118
### **Java**
92119

93120
```java
121+
class Solution {
122+
public boolean validTicTacToe(String[] board) {
123+
int x = 0, o = 0;
124+
for (int i = 0; i < 3; i++) {
125+
for (int j = 0; j < 3; j++) {
126+
if (board[i].charAt(j) == 'X') {
127+
x++;
128+
} else if (board[i].charAt(j) == 'O') {
129+
o++;
130+
}
131+
}
132+
}
133+
if (x != o && x - 1 != o) {
134+
return false;
135+
}
136+
if (win(board, 'X') && x - 1 != o) {
137+
return false;
138+
}
139+
return !(win(board, 'O') && x != o);
140+
}
141+
142+
private boolean win(String[] b, char p) {
143+
for (int i = 0; i < 3; i++) {
144+
if (b[i].charAt(0) == p && b[i].charAt(1) == p && b[i].charAt(2) == p) {
145+
return true;
146+
}
147+
if (b[0].charAt(i) == p && b[1].charAt(i) == p && b[2].charAt(i) == p) {
148+
return true;
149+
}
150+
}
151+
if (b[0].charAt(0) == p && b[1].charAt(1) == p && b[2].charAt(2) == p) {
152+
return true;
153+
}
154+
return b[0].charAt(2) == p && b[1].charAt(1) == p && b[2].charAt(0) == p;
155+
}
156+
}
157+
```
158+
159+
### **Go**
160+
161+
```go
162+
func validTicTacToe(board []string) bool {
163+
x, o := 0, 0
164+
for i := 0; i < 3; i++ {
165+
for j := 0; j < 3; j++ {
166+
if board[i][j] == 'X' {
167+
x++
168+
} else if board[i][j] == 'O' {
169+
o++
170+
}
171+
}
172+
}
173+
if x != o && x-1 != o {
174+
return false
175+
}
176+
if win(board, 'X') && x-1 != o {
177+
return false
178+
}
179+
return !(win(board, 'O') && x != o)
180+
}
181+
182+
func win(b []string, p byte) bool {
183+
for i := 0; i < 3; i++ {
184+
if b[i][0] == p && b[i][1] == p && b[i][2] == p {
185+
return true
186+
}
187+
if b[0][i] == p && b[1][i] == p && b[2][i] == p {
188+
return true
189+
}
190+
}
191+
if b[0][0] == p && b[1][1] == p && b[2][2] == p {
192+
return true
193+
}
194+
return b[0][2] == p && b[1][1] == p && b[2][0] == p
195+
}
196+
```
94197

198+
### **C++**
199+
200+
```cpp
201+
class Solution {
202+
public:
203+
bool validTicTacToe(vector<string>& board) {
204+
int x = 0, o = 0;
205+
for (int i = 0; i < 3; ++i) {
206+
for (int j = 0; j < 3; ++j) {
207+
if (board[i][j] == 'X') {
208+
++x;
209+
} else if (board[i][j] == 'O') {
210+
++o;
211+
}
212+
}
213+
}
214+
if (x != o && x - 1 != o) {
215+
return false;
216+
}
217+
if (win(board, 'X') && x - 1 != o) {
218+
return false;
219+
}
220+
return !(win(board, 'O') && x != o);
221+
}
222+
223+
bool win(vector<string>& b, char p) {
224+
for (int i = 0; i < 3; ++i) {
225+
if (b[i][0] == p && b[i][1] == p && b[i][2] == p) {
226+
return true;
227+
}
228+
if (b[0][i] == p && b[1][i] == p && b[2][i] == p) {
229+
return true;
230+
}
231+
}
232+
if (b[0][0] == p && b[1][1] == p && b[2][2] == p) {
233+
return true;
234+
}
235+
return b[0][2] == p && b[1][1] == p && b[2][0] == p;
236+
}
237+
};
95238
```
96239

97240
### **...**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
bool validTicTacToe(vector<string>& board) {
4+
int x = 0, o = 0;
5+
for (int i = 0; i < 3; ++i) {
6+
for (int j = 0; j < 3; ++j) {
7+
if (board[i][j] == 'X') {
8+
++x;
9+
} else if (board[i][j] == 'O') {
10+
++o;
11+
}
12+
}
13+
}
14+
if (x != o && x - 1 != o) {
15+
return false;
16+
}
17+
if (win(board, 'X') && x - 1 != o) {
18+
return false;
19+
}
20+
return !(win(board, 'O') && x != o);
21+
}
22+
23+
bool win(vector<string>& b, char p) {
24+
for (int i = 0; i < 3; ++i) {
25+
if (b[i][0] == p && b[i][1] == p && b[i][2] == p) {
26+
return true;
27+
}
28+
if (b[0][i] == p && b[1][i] == p && b[2][i] == p) {
29+
return true;
30+
}
31+
}
32+
if (b[0][0] == p && b[1][1] == p && b[2][2] == p) {
33+
return true;
34+
}
35+
return b[0][2] == p && b[1][1] == p && b[2][0] == p;
36+
}
37+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func validTicTacToe(board []string) bool {
2+
x, o := 0, 0
3+
for i := 0; i < 3; i++ {
4+
for j := 0; j < 3; j++ {
5+
if board[i][j] == 'X' {
6+
x++
7+
} else if board[i][j] == 'O' {
8+
o++
9+
}
10+
}
11+
}
12+
if x != o && x-1 != o {
13+
return false
14+
}
15+
if win(board, 'X') && x-1 != o {
16+
return false
17+
}
18+
return !(win(board, 'O') && x != o)
19+
}
20+
21+
func win(b []string, p byte) bool {
22+
for i := 0; i < 3; i++ {
23+
if b[i][0] == p && b[i][1] == p && b[i][2] == p {
24+
return true
25+
}
26+
if b[0][i] == p && b[1][i] == p && b[2][i] == p {
27+
return true
28+
}
29+
}
30+
if b[0][0] == p && b[1][1] == p && b[2][2] == p {
31+
return true
32+
}
33+
return b[0][2] == p && b[1][1] == p && b[2][0] == p
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /