【LeetCode】794. Valid Tic-Tac-Toe State 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/valid-tic-tac-toe-state/description/
题目描述:
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”}.
题目大意
判断一个棋盘是不是有效的井字棋的状态。
解题方法
判断是否是个合法的状态,我只需要排除不合法的状态就好了。不合法的状态分为三种情况:
- 初始的棋盘上O的个数不等于X的个数,或者O的个数不等于X-1;
- 棋盘上O的个数等于X - 1(轮到O下),但是O还没下棋,此时O已经赢了;
- 棋盘上O的个数等于X(轮到X下),但是X还没下棋,此时X已经赢了;
除此之外,就能下棋,或者棋局已经结束。
时间复杂度是O(N^2),空间复杂度是O(1)。N是棋盘变长。
class Solution:
def validTicTacToe(self, board):
"""
:type board: List[str]
:rtype: bool
"""
xCount, oCount = 0, 0
for i in range(3):
for j in range(3):
if board[i][j] == 'O':
oCount += 1
elif board[i][j] == 'X':
xCount += 1
if oCount != xCount and oCount != xCount - 1: return False
if oCount != xCount and self.win(board, 'O'): return False
if oCount != xCount - 1 and self.win(board, 'X'): return False
return True
def win(self, board, P):
# board is list[str]
# P is 'X' or 'O' for two players
for j in range(3):
if all(board[i][j] == P for i in range(3)): return True
if all(board[j][i] == P for i in range(3)): return True
if board[0][0] == board[1][1] == board[2][2] == P: return True
if board[0][2] == board[1][1] == board[2][0] == P: return True
return False
参考资料:
日期
2018 年 10 月 14 日 —— 美好的周一怎么会出现雾霾呢?
【LeetCode】794. Valid Tic-Tac-Toe State 解题报告(Python)的更多相关文章
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 【leetcode】1275. Find Winner on a Tic Tac Toe Game
题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe
题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
随机推荐
- SpringBoot整合Shiro 三:整合Mybatis
搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 整合Mybatis 添加Maven依赖 mysql.dr ...
- spring整合mybatis — 更新完毕
1.准备工作 -- 导入依赖 <dependency> <groupId>org.springframework</groupId> <artifactId& ...
- CAS你知道吗
1.比较并交换 CASDemo /** * CAS => compareAndSet * 比较并交换 */ public class CASDemo { public static void m ...
- js中!!的妙用
0.-0.null."".false.undefined 或者 NaN转化为false,其他为true
- [php代码审计] 通读审计之shangfancms
前言 大部分的MVC框架,访问的控制器大部分是由外部参数来决定的,那么本篇文章所通读的MVC框架与之前的一系列MVC框架不太一样,它的路由是由程序本身的路由表来决定的. 源码下载 https://ww ...
- SpringMVC注解详情
@Component.@Repository @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller 控制层,就是我们的action层 @Service ...
- 【Java 8】Stream API
转自 Java 8 Stream Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式.Stream 是Java8中处理集合的关键抽象概念,它可以对集合进 ...
- 使用RabbitMQ搭建MQTT服务
由于近期公司需要搭建一套物联网采集环境,底层设备采用MQTT协议传输数据.服务器环境为linux,考虑到现有环境已经有RabbitMQ环境,Rabbit是基于AMQP协议开发的一套高效的消息传输队列. ...
- Sqlite 常用操作及使用EF连接Sqlite
Sqlite是一个很轻,很好用的数据库.兼容性很强,由于是一款本地文件数据库,不需要安装任何数据库服务,只需引入第三方开发包就可以.Sqlite的处理速度比MySql和PostgreSQL更快,性能很 ...
- Redis哨兵日常维护
目录 一.日常操作 指定一个从做新主 添加一个从节点 添加一个Setinel节点 一.日常操作 指定一个从做新主 有时候需要将当前主节点机器下线,并指定一个高一些性能的从节点接替 将其它从节点的sla ...