找出井字棋的获胜者

思路

模拟。

代码

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的更多相关文章

  1. LeetCode 第 165 场周赛

    LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...

  2. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  4. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  5. 拼写单词[哈希表]----leetcode周赛150_1001

    题目描述: 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我 ...

  6. 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)

    Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...

  7. 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)

    Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...

  8. 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

    Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...

  9. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

随机推荐

  1. 在 Asp.Net Core 中安装 MVC

    在 ASP.NET Core 中安装 MVC 到目前为止,我们在本系列视频中使用的 ASP.NET Core 项目是使用“空”项目模板生成的.目前这个项目没有设置和安装 MVC. 两个步骤学会在 AS ...

  2. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  3. dogcom在openwrt上的使用

    前提,先配置并运行mentohust(作为802.1x认证) 1,取得编译完成的可执行文件(可先在虚拟机里测试) 2,上传到路由器 3,把dogcom主程序和配置文件放在/etc/storage/do ...

  4. Python连载27-log日志

    一.log 1.推荐网站:https://www.cnblogs.com/yyds/p/6901864.html 该网站为日志处理logging模块简介 2.logging模块提供模块级别的函数记录日 ...

  5. 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 ...

  6. RocketMQ 使用情况梳理

    个人梳理有限:欢迎大家 丰富此文档 2018年 12 月 RocketMQ 版本  不适用于 新版关系请勿参考目前规划原则:          topic 创建基于业务  消费者基于模块 多对多关系 ...

  7. PostgreSQL事务特性之嵌套事务

    嵌套事务的实现是基于SAVEPOINT.ROLLBACK TO SAVEPOINT和RELEASE SAVEPOINT的,也就是设置一个保存点,可以回滚到保存点和释放保存点. 测试表的初始状态如下: ...

  8. Windows git和cmd代理设置

    linux的比较简单,直接修改配置文件即可,这里就不再赘述 设置Git代理 http代理: 临时代理: export http_proxy=http://127.0.0.1:7777 export h ...

  9. javascript(六)运算符

    运算符概述 JavaScript中的运算符用于算术表达式. 比较表达式. 逻辑表达式. 赋值表达式等.需要注意的是, 大多数运算符都是由标点符号表示的, 比如 "+" 和" ...

  10. 2019 学霸君java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.学霸君等公司offer,岗位是Java后端开发,因为发展原因最终选择去了学霸君,入职一年时间了,也成为了面试官 ...