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 ...
随机推荐
- [LeetCode] 854. K-Similar Strings 相似度为K的字符串
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two ...
- Pytorch循环神经网络LSTM时间序列预测风速
#时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺序的,同样大 ...
- Java中HashMap和TreeMap的区别
什么是Map集合在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.这就是我们平时说的键值对. HashMap ...
- golang 1.12 自动补全
golang 1.12 版本的自动补全问题 问题 golang 1.12 开始, 默认的 go install 不再生成 pkg 文件. 所以对第三方库的引用, 无法进行代码的自动补全. 解决方法 g ...
- hystrixDashboard(服务监控)
1.新建项目 microservicecloud-consumer-hystrix-dashboard 2.yml文件 server: port: 9001 3.在pom.xml文件增加如下内容 &l ...
- 拼数(C++)
问题: 设有n个正整数,将他们排成一排,组成一个最大的多位整数. INPUT: 第一行,正整数的个数n 第二行,n个正整数 OUTPUT: 一个正整数,表示最大的整数. 输入样例: 3 13 312 ...
- windows10安装ubuntu双系统教程(初稿)
windows10安装ubuntu双系统教程(绝对史上最详细) Win10 Ubuntu16.04/Ubuntu18.04双系统完美安装 Windows10+Ubuntu18.04双系统安装成功心得( ...
- 集合类源码(四)Collection之BlockingQueue(ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue)
ArrayBlockingQueue 功能 全名 public class ArrayBlockingQueue<E> extends AbstractQueue<E> imp ...
- Phaser铁人三项
/** * 模拟铁人三项 */ public class PhaserTest { private static Random random = new Random(System.currentTi ...
- Ajax 跨域请求,Chrome 无法显示 Set-Cookie
在使用 Ajax 进行跨域请求时,前后端均已设置 withCredentials = true,但 Chrome 前端响应无法显示 Set-Cookie. 一开始以为 Cookie 并没有设置成功,但 ...