LeetCode1275. 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. The rules of Tic-Tac-Toe are:

Players take turns placing characters into empty squares ’ '.
The first player A always places ‘X’ characters, while the second player B always places ‘O’ characters.
‘X’ and ‘O’ characters are always placed into empty squares, never on filled ones.
The game ends when there are three 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.
Given a 2D integer array moves where moves[i] = [rowi, coli] indicates that the ith move will be played on grid[rowi][coli]. return the winner of the game if it exists (A or B). In case the game ends in a draw return “Draw”. If there are still movements to play return “Pending”.

You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.

Example 1:

Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output: “A”
Explanation: A wins, they always play first.
Example 2:

Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
Output: “B”
Explanation: B wins.
Example 3:

Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
Output: “Draw”
Explanation: The game ends in a draw since there are no moves to make.

Constraints:

1 <= moves.length <= 9
moves[i].length == 2
0 <= rowi, coli <= 2
There are no repeated elements on moves.
moves follow the rules of tic tac toe.

二、题解

class Solution {
public:string tictactoe(vector<vector<int>>& moves) {int n = moves.size();vector<vector<string>> grid(3,vector<string>(3," "));for(int i = 0;i < n;i++){if(i % 2 == 0) grid[moves[i][0]][moves[i][1]] = "X";else grid[moves[i][0]][moves[i][1]] = "O";}//判断行for(int i = 0;i < 3;i++){if(grid[i][0] != " " && grid[i][0] == grid[i][1] && grid[i][1] == grid[i][2]){if(grid[i][0] == "X") return "A";else return "B";}}//判断列for(int j = 0;j < 3;j++){if(grid[0][j] != " " && grid[0][j] == grid[1][j] && grid[1][j] == grid[2][j]){if(grid[0][j] == "X") return "A";else return "B";}}//判断主对角线if(grid[0][0] != " " && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]){if(grid[0][0] == "X") return "A";else return "B";}//判断副对角线if(grid[0][2] != " " && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0]){if(grid[0][2] == "X") return "A";else return "B";}if(moves.size() < 9) return "Pending";else return "Draw";}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/589161.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Keras实现Transformer

# 导入所需的库 import numpy as np from keras.models import Model from keras.layers import Input, Dense, Embedding, MultiHeadAttention from keras.optimizers import Adam# 定义模型参数 vocab_size 10000 # 词汇表大小 embedding_dim 256 # 嵌入维度 num_heads …

营销系统升级:运荔枝无代码集成电商API功能

无代码开发&#xff1a;运荔枝连接电商与CRM 随着电子商务的持续扩张&#xff0c;企业亟需无缝集成电商平台与客户关系管理&#xff08;CRM&#xff09;系统&#xff0c;以提高运营效率。运荔枝通过其无代码开发平台&#xff0c;为企业提供了简化的API连接服务。商家可以在不具…

Prometheus 监控进程

prometheus 进程的监控 1. process exporter功能 2. 监控目标对主机进程的监控&#xff0c;chronyd sshd 等服务进程已经已定义脚本运行程序的运行状态监控。 process-compose的安装 监控所有进程 mkdir /data/process_exporter -p cd /data/process_exporter创建配置文件 …

Linux期末复习笔记

期末复习笔记 引言目录操作用户和组用户组 文件及文件权限文件文件目录及分类Linux文件目录文件类型文件权限 磁盘管理磁盘命名规则使用命令行工具管理磁盘分区和文件系统linux中的数据备份策略软件包安装检查维护文件系统 进程管理进程分类ps查看与top查看的区别&#xff1a; …

为什么ChatGPT选择了SSE,而不是WebSocket?

我在探索ChatGPT的使用过程中&#xff0c;发现了一个有趣的现象&#xff1a;ChatGPT在实现流式返回的时候&#xff0c;选择了SSE&#xff08;Server-Sent Events&#xff09;&#xff0c;而非WebSocket。 那么问题来了&#xff1a;为什么ChatGPT选择了SSE&#xff0c;而不是We…

力扣25题: K 个一组翻转链表

【题目链接】力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台&#xff0c;解题代码如下&#xff1a; class Solution {public ListNode reverseKGroup(ListNode head, int k) {ListNode curNode head;ListNode groupHead, groupTail head, lastGrou…

UART通信协议:串行通信的精华

UART通信协议&#xff1a;串行通信的精华 UART&#xff08;Universal Asynchronous Receiver/Transmitter&#xff09;通信协议是一种广泛应用于串行通信的标准&#xff0c;它在电子设备和嵌入式系统中扮演着至关重要的角色。本文将深入介绍UART通信协议的基本原理、工作方式、…

一个可以用于生产环境得PHP上传函数

上传表单 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><title>文件上传</title> </head> <body><h1>选择要上传的文件</h1><!-- 定义一个包含文件输入字段的表单 --…

[每周一更]-(第46期):Linux下配置Java所需环境及Java架构选型

Linux下配置Java所需环境及Java架构选型 一、配置基础环境 1.配置tomcat 环境变量 wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.8/src/apache-tomcat-10.1.8-src.tar.gz tar -zxvf apache-tomcat-10.1.8-src.tar.gz 在/etc/profile 末尾追加export CATALINA_HOME…

异常控制流ECF

大家好&#xff0c;我叫徐锦桐&#xff0c;个人博客地址为www.xujintong.com&#xff0c;github地址为https://github.com/xjintong。平时记录一下学习计算机过程中获取的知识&#xff0c;还有日常折腾的经验&#xff0c;欢迎大家访问。 一、异常控制流&#xff08;ECF) 现代系…

[BUG]Datax写入数据到psql报不能序列化特殊字符

1.问题描述 Datax从mongodb写入数据到psql报错如下 org.postgresql.util.PSQLException: ERROR: invalid bytesequence for encoding "UTF8": 0x002.原因分析 此为psql独有的错误&#xff0c;不能对特殊字符’/u0000’,进行序列化&#xff0c;需要将此特殊字符替…

webrtc中的接口代理框架

文章目录 接口代理框架Proxy体系类结构导出接口 webrtc的实际运用PeerConnectionFactoyPeerConnection使用 接口代理框架 webrtc体系庞大&#xff0c;模块化极好&#xff0c;大多数模块都可以独立使用。模块提供接口&#xff0c;外部代码通过接口来使用模块功能。 在webrtc中通…

uni-app 前后端调用实例 基于Springboot

锋哥原创的uni-app视频教程&#xff1a; 2023版uniapp从入门到上天视频教程(Java后端无废话版)&#xff0c;火爆更新中..._哔哩哔哩_bilibili2023版uniapp从入门到上天视频教程(Java后端无废话版)&#xff0c;火爆更新中...共计23条视频&#xff0c;包括&#xff1a;第1讲 uni…

《PCI Express体系结构导读》随记 —— 第I篇 第2章 PCI总线的桥与配置(1)

前言中曾提到&#xff1a;本章重点介绍PCI桥。 在PCI体系结构中含有两类桥&#xff1a;一类是HOST主桥&#xff1b;另一类是PCI桥。在每一个PCI设备中&#xff08;包括PCI桥&#xff09;&#xff0c;都含有一个配置空间。这个配置空间由HOST主桥管理&#xff0c;而PCI桥可以转…

cfa一级考生复习经验分享系列(十五)

备考背景&#xff1a; 本科211石油理科背景&#xff1b;无金融方面专业知识及工作经验&#xff1b;在职期间备考&#xff1b;有效备考时间2个月&#xff1b;12月一级考试10A。 复习进度及教材选择 首先说明&#xff0c;关于教材的经验分享针对非金融背景考生。 第一阶段&#x…

Java EE Servlet之Cookie 和 Session

文章目录 1. Cookie 和 Session1.1 Cookie1.2 理解会话机制 (Session)1.2.1 核心方法 2. 用户登录2.1 准备工作2.2 登录页面2.3 写一个 Servlet 处理上述登录请求2.4 实现登录后的主页 3. 总结 1. Cookie 和 Session 1.1 Cookie cookie 是 http 请求 header 中的一个属性 浏…

[枚举涂块]画家问题

画家问题 题目描述 有一个正方形的墙&#xff0c;由N*N个正方形的砖组成&#xff0c;其中一些砖是白色的&#xff0c;另外一些砖是黄色的。Bob是个画家&#xff0c;想把全部的砖都涂成黄色。但他的画笔不好使。当他用画笔涂画第(i, j)个位置的砖时&#xff0c; 位置(i-1, j)、…

劫持 PE 文件:新建节表并插入指定 DLL 文件

PE格式简介 PE(Portable Executable)格式&#xff0c;是微软Win32环境可移植可执行文件(如exe、dll、vxd、sys和vdm等)的标准文件格式。PE格式衍生于早期建立在VAX(R)VMS(R)上的COFF(Common Object File Format)文件格式。 Portable 是指对于不同的Windows版本和不同的CPU类型上…

UIToolKit使用心得

起因 因为那个uitoolkit自己写了一套graphView&#xff0c;所以想着来用用但是用完之后发现也不过如此 怎么构建自己的组件 我在继承Node之后想修改node的样式该怎么办呢是这样的。先用pick点击默认的node节点元素- 在pick默认创建的node节点之后&#xff0c;可以把它的uxml…

类的加载顺序问题-demo展示

面试的的时候经常会被问到包含静态代码块、实例代码块和构造器等代码结构的加载顺序问题&#xff0c;下面借用一个面试题&#xff0c;回顾一下类的代码加载顺序。 public class AooTest {public static void main(String[] args) {AooTest.f1();}static AooTest test1 new Ao…