LeetCode Weekly Contest 22
1. 532. K-diff Pairs in an Array
分析:由于要唯一,所以要去重,考虑k=0,时候,相同的数字需要个数大于1,所以,先用map统计个数,对于k=0,特判,对于其他的,遍历每一个数,只需要判断a + k是否存在即可。
这个题目,是absolute difference,我没注意这点,没有判断k<0,直接return 0,导致出错。
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
if(k < ) return ;
int n = nums.size();
int res = ;
map<int, int> ma;
for (int x : nums) {
ma[x]++;
}
if(k == ) {
for (auto it : ma) {
if(it.second > ) res++;
}
return res;
}
for (auto it : ma) {
int x = it.first;
if(ma.count(x + k)) res++;
}
return res;
}
};
2. 531. Lonely Pixel I
预处理出行列元素个数,然后扫描每一个元素进行处理,比较简单。
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& p) {
int n = p.size();
if(n == ) return ;
int m = p[].size();
if(m == ) return ;
vector<int> row(n, ), col(m, );
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if(p[i][j] == 'B') {
row[i]++;
col[j]++;
}
}
}
int res = ;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if(p[i][j] == 'B') {
if(row[i] == && col[j] == ) res++;
}
}
}
return res;
}
};
3. 533. Lonely Pixel II
也是要先预处理出每行,每列的b的个数,然后进行处理。
分析这个题目,是要按列进行考虑的,因为满足要求的b的列的位置上为b的这些行要相同,所以如果只要有一个不满足要求,这一列的b都是不满足要求的。
同时注意到,每2行可能需要比较多次,每次都是相同重复的,可以记录下来,进行加速处理。也算比较简单。
class Solution {
public:
int findBlackPixel(vector<vector<char>>& p, int k) {
int n = p.size();
if(n == ) return ;
int m = p[].size();
if(m == ) return ;
vector<int> row(n, ), col(m, );
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if(p[i][j] == 'B') {
row[i]++;
col[j]++;
}
}
}
int res = ;
map<pair<int, int>, bool> ma;
for (int j = ; j < m; j++) {
if(col[j] != k) continue;
int id = -;
bool f = ;
for (int i = ; i < n; i++) {
if(p[i][j] == 'B') {
if(row[i] != k) {
f = ;break;
}
if(id == -) id = i;
else {
if(ma.count({id, i})) {
if(!ma[{id, i}]) {
f = ;
break;
}
}
for (int x = ; x < m; x++) {
if(p[id][x] != p[i][x]) {
ma[{id, i}] = ;
f = ;
break;
}
}
if(f) {
ma[{id, i}] = ;
} else
break;
}
}
}
if(f) res += k;
}
return res;
}
};
4. 514. Freedom Trail
分析:读懂题意,这么多英文描述,我一看,脑袋都花了!
然后,我想着是不是贪心,每次都转移到最近的一个字母,但是提交以后,wa。然后观察例子,分析出是dp。
因为这一次的转移会影响下一次的转移,局部是最优的,但是不能保证全局最优,所以需要记录所有的状态,然后进行处理。
考虑合法的转移状态,注意非法的情况。还有,这题的数据范围很小,100,100的,如果是贪心,那也太小了,所以,一般是dp的可能性很大,最后dp的复杂度是100*100*100,可以在1s的实现内运行完毕。
class Solution {
public:
int findRotateSteps(string ring, string key) {
int n = ring.size();
int m = key.size();
if(n == ) {
return m;
}
vector<vector<int>> dp(m + , vector<int>(n + , -));
dp[][] = ;
for (int i = ; i <= m; i++) {
char cur = key[i - ];
for (int j = ; j < n; j++) {
if(dp[i - ][j] == -) continue;
for (int x = ; x < n; x++) {
if(ring[x] == cur) {
int y = abs(x - j);
if(n - y < y) y = n - y;
if(dp[i][x] == -)
dp[i][x] = dp[i - ][j] + y;
else
dp[i][x] = min(dp[i][x], dp[i - ][j] + y);
}
//cout << j << " " << x << " " << i << " "<< dp[i][x] << endl;
}
}
}
int res = INT_MAX;
for (int i = ; i < n; i++)
if(dp[m][i] != -)
res = min(res, dp[m][i]);
return res + m;
}
};
要求最小值,dp非法值处理的-1的情况,代码写的比较复杂,应该处理成INT_MAX,这样非法情况可以像正常情况一样处理,但是要注意溢出哦,可以取一个很大的数,而不是INT_MAX就行了。
这次题目不是很难,但是我写的还是比较慢!还需要多练!
LeetCode Weekly Contest 22的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- 《计算机图形学基础(OpenGL版)》勘误表
第1版第1次印刷: 所在页码 所在行 原内容 更正为 41 16 k=Δx/Δy k=Δy/Δx 46 6 s-t=2Δy/Δx(xi+1)+2b+2yi-1 s-t=2Δy/Δx(xi+1)+2b- ...
- C# 检测dll的新版本号方法
FileVersionInfo info = FileVersionInfo.GetVersionInfo(YourFileNameHere);string version = info.FileMa ...
- Delphi 不用标题栏移动窗体
procedure TxxxxForm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: I ...
- spring boot注解
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- mongodb集群搭建(分片+副本)
原文地址:https://www.cnblogs.com/ityouknow/p/7344005.html 相关概念 先来看一张图: 从图中可以看到有四个组件:mongos.config server ...
- CentOS7.4上搭建rocketMQ集群
一.rocketMQ集群部署方案优缺点对比: 多Master模式(2m-noslave) : 一个集群无Slave,全是Master,例如2个Master或者3个Master 优点:配置简单,单个Ma ...
- 爬虫系列(十三) 用selenium爬取京东商品
这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...
- 自己总结的php开发中用到的工具
需要一个编辑器IDE,推荐用phpstorm. IDE安装完了,还要搞个Xdebug,这个很有用,程序断点跟踪调试就靠他了. phpstom平时使用的时候,编辑界面感觉很枯燥的时候,可以换个主题,换主 ...
- firebird的递归查询
with RECURSIVE cte as ( select a.* from PM_PROJECT a where a.pm_id='root_id' union all select k.* fr ...
- UVALive 6177 The King's Ups and Downs
The King's Ups and Downs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UV ...