【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 ...
随机推荐
- 49-Reverse Linked List II
Reverse Linked List II My Submissions QuestionEditorial Solution Total Accepted: 70579 Total Submiss ...
- Hadoop 相关知识点(一)
作业提交流程(MR执行过程) Mapreduce2.x Client:用来提交作业 ResourceManager:协调集群上的计算资源的分配 NodeManager:负责启动和监控集群上的计算容器( ...
- 大数据学习day36-----flume02--------1.avro source和kafka source 2. 拦截器(Interceptor) 3. channel详解 4 sink 5 slector(选择器)6 sink processor
1.avro source和kafka source 1.1 avro source avro source是通过监听一个网络端口来收数据,而且接受的数据必须是使用avro序列化框架序列化后的数据.a ...
- 大数据学习day21-----spark04------1. 广播变量 2. RDD中的cache 3.RDD的checkpoint方法 4. 计算学科最受欢迎老师TopN
1. 广播变量 1.1 补充知识(来源:https://blog.csdn.net/huashetianzu/article/details/7821674) 之所以存在reduce side jo ...
- git提交指定文件
1. 用git add 命令添加第一个commit需要的文件 git add file1 git add file2 2. 隐藏其他修改,git stash 的参数中 -k 开关告诉仓库保持文件的完整 ...
- JAVA中的六种日期类型使用
基本的6种日期类 /** * 六种时间类型的类 * 数据库格式的时间三种格式 */ java.util.Date date = new java.util.Date();//年与日时分秒 //数据库的 ...
- 复制virtualbox虚拟硬盘
D:\VirtualBox\VBoxManage.exe clonevdi F:\virtualbox\rac1\rac1.vdi F:\virtualbox\rac2\rac2.vdi 虚拟机软件安 ...
- Android Https相关完全解析
转载: 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48129405: 本文出自:[张鸿洋的博客] 一.概述 其实这篇文章理论 ...
- Logback设置保留日志文件个数
Logback日志文件占用存储空间太多,设置保留文件个数,清理之前的文件. 主要由如下三个参数配合使用 maxHistory ,可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件,,例如设置 ...
- 【Linux】【Services】【SaaS】 kubeadm安装kubernetes
1. 简介 2. 环境 2.1. OS: CentOS Linux release 7.5.1804 (Core) 2.2. Ansible: 2.6.2-1.el7 2.3. docker: 2. ...