Leetcode周赛165
找出井字棋的获胜者
思路
模拟。
代码
class Solution {
public:
char mp[4][4];
bool check(int x, int y, char ch) {
int flag = 1;
for(int i = 0; i < 3; ++i) {
if(mp[x][i] != ch) flag = 0;
}
if(flag) return true;
flag = 1;
for(int i = 0; i < 3; ++i) {
if(mp[i][y] != ch) flag = 0;
}
if(flag) return true;
int cnt = 0;
for(int i = -2; i < 3; ++i) {
if(x + i >= 0 && x + i < 3 && y + i >= 0 && y + i < 3 && mp[x+i][y+i] == ch) ++cnt;
}
if(cnt == 3) return true;
cnt = 0;
for(int i = -2; i < 3; ++i) {
if(x - i >= 0 && x - i < 3 && y + i >= 0 && y + i < 3 && mp[x-i][y+i] == ch) ++cnt;
}
return cnt == 3;
}
string tictactoe(vector<vector<int>>& moves) {
int n = moves.size();
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) mp[i][j] = ' ';
}
for(int i = 0; i < n; ++i) {
if(i & 1) mp[moves[i][0]][moves[i][1]] = 'O';
else mp[moves[i][0]][moves[i][1]] = 'X';
}
int flag = 0;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
if(mp[i][j] != ' ' && check(i, j, mp[i][j])) {
if(mp[i][j] == 'X') return "A";
else if(mp[i][j] == 'O') return "B";
}
if(mp[i][j] == ' ') flag = 1;
}
}
if(flag) return "Pending";
else return "Draw";
}
};
不浪费原料的汉堡制作方案
思路
一开始看到数据范围只有\(10^7\)然后直接枚举\(T\)了(可能是写搓了?),然后写了个二分。
二分有多少个小皇堡然后比较\(tomatoSlices\)使用数量。
代码
class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
std::vector<int> v;
int ub = cheeseSlices, lb = 0, mid, ans = -1;
while(ub >= lb) {
mid = (ub + lb) >> 1;
if(mid * 2 + (cheeseSlices - mid) * 4 >= tomatoSlices) {
lb = mid + 1;
ans = mid;
} else ub = mid - 1;
}
if(ans != -1 && ans * 2 + (cheeseSlices - ans) * 4 == tomatoSlices) {
v.resize(2);
v[0] = cheeseSlices - ans, v[1] = ans;
}
return v;
}
};
统计全为 1 的正方形子矩阵
思路
二维前缀和然后枚举上下边界的左边界,看这个正方形内的\(1\)的个数。
代码
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
int sum[305][305];
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= m; ++j) sum[i][j] = 0;
}
int ans = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + matrix[i][j];
}
}
for(int l = 1; l <= n; ++l) {
for(int r = l; r <= n; ++r) {
int len = r - l + 1;
for(int j = 1; j + len - 1 <= m; ++j) {
if(sum[r][j+len-1] - sum[l-1][j+len-1] - sum[r][j-1] + sum[l-1][j-1] == len * len) ++ans;
}
}
}
return ans;
}
};
分割回文串 III
思路
先预处理出以\(i\)为左端点,\(j\)为右端点的字符串变成回文串需要修改多少个位置。
然后进行\(dp\),\(dp[i][j]\)表示前\(i\)个字母构成\(j\)个回文串所需要修改的最少位置。
转移方程为\(dp[i][j]=min(dp[i][j],dp[lst-1][j-1]+change[lst][i])\)。
代码
class Solution {
public:
const int inf = 0x3f3f3f3f;
int palindromePartition(string s, int k) {
int dp[105][105];
memset(dp, inf, sizeof(dp));
int n = s.length();
for(int i = 0; i <= k; ++i) dp[0][i] = 0;
int change[105][105];
memset(change, 0, sizeof(change));
for(int i = 0; i < n; ++i) {
for(int j = i; j < n; ++j) {
for(int st = i, ed = j; st <= ed; ++st, --ed) {
change[i][j] += (s[st] != s[ed]);
}
}
}
for(int i = 0; i < n; ++i) {
for(int j = 1; j <= min(i+1, k); ++j) {
for(int lst = 1; lst <= i + 1; ++lst) {
dp[i+1][j] = min(dp[i+1][j], dp[lst-1][j-1] + change[lst-1][i]);
}
}
}
return dp[n][k];
}
};
Leetcode周赛165的更多相关文章
- LeetCode 第 165 场周赛
LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 拼写单词[哈希表]----leetcode周赛150_1001
题目描述: 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我 ...
- 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)
Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...
- 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)
Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...
- 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)
Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
随机推荐
- 【转】TCP/IP协议——ARP详解
本文主要讲述了ARP的作用.ARP分组格式.ARP高速缓存.免费ARP和代理ARP. 1.学习ARP前要了解的内容 建立TCP连接与ARP的关系 应用接受用户提交的数据,触发TCP建立连接,TCP的第 ...
- mysql 聚集索引,非聚集索引,覆盖索引区别。
把原站信息经过筛选贴过来,用于自己备忘.原站:https://www.cnblogs.com/aspwebchh/p/6652855.html ---------------------------- ...
- oracle--数据库扩容后出现ORA-27102
一,问题描述 Connected to an idle instance. SQL> startup nomount ORA: obsolete or deprecated parameter( ...
- Ubuntu18.04开机挂载硬盘
一.查看磁盘信息 fstab文件的格式如上,其中: 1.设备文件名称是指磁盘或分区的文件名称,也可以使用label或uuid.UUID可以通过"sudo blkid"命令 ...
- 各版本linux推荐的软件源
x64 Ubuntu 18.4 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe mul ...
- linux安装go环境并编写第一个go程序
1.从官网下载go源码包 wget https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz 2.在/usr/local下解压源码包 sudo tar ...
- Field redisTemplate in xxxxxx required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
*************************** APPLICATION FAILED TO START *************************** Description: Fie ...
- golang学习笔记 --flag
概述 flag包提供了一系列解析命令行参数的功能接口 命令行语法 命令行语法主要有以下几种形式 -flag //只支持bool类型 -flag=x -flag x //只支持非bool类型 以上语法对 ...
- 《 .NET并发编程实战》阅读指南 - 第7章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- WPF 精修篇 倾斜 SkewTransform
原文:WPF 精修篇 倾斜 SkewTransform 倾斜 SkewTransform AngleX 倾斜X角度 AngleY 倾斜Y角度 CenterX CenterY 中心点 <Stack ...