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 ...
随机推荐
- Caffe: Vs13添加CUDA支持
1. 右键工程 点击:Building Dependency 右击:Build Customizations 点击选项:CUDA 7.5 2.添加C++依赖: cudart.lib kernel32 ...
- RN-第三方之react-native-pull 下拉刷新、上拉加载
有一个很好的下拉刷新.上拉加载库:react-native-pull地址:https://github.com/greatbsky/react-native-pull-demo 使用 import { ...
- 从输入url到页面展示出来经历了哪些过程
本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址. 此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...
- 通俗易懂之SpringMVC&Struts2前端拦截器详解
直接进入主题吧!一,配置Struts2的拦截器分两步走1配置对应的拦截器类:2在配置文件Struts.xml中进行配置拦截器同时在Strust2中配置拦截器类有三种方法1实现Interceptor接口 ...
- lucene7.1.0实现搜索文件内容
Lucene的使用主要体现在两个步骤: 1 创建索引,通过IndexWriter对不同的文件进行索引的创建,并将其保存在索引相关文件存储的位置中. 2 通过索引查寻关键字相关文档. 首先,我们需要定义 ...
- IDEA返回上一步
在开发中进入一个方法后想要到原来那行 ctrl+alt+左 回到上一步 ctrl+alt+右 回到下一步
- js中writeln()方法
writeln( ) 方法与 write( ) 方法几乎一样,差别仅在于是前者将在所提供的任何字符串后添加一个换行符.在 HTML 中,这通常只会在后面产生一个空格:不过如果使用了 <PRE&g ...
- 在vue项目中引用element-ui时 让el-input 获取焦点的方法
在制作项目的时候遇到一个需求,点击一个按钮弹出一个input输入框,并让输入框获得焦点,项目中引用了ElementUI 在网上查找了很多方法,但是在实际使用中发现了一个问题无论是使用$ref获取inp ...
- redis命令学习的注意问题
1.set get命令只用于字符串,get命令取key值时string正常返回,没有key返回nil,其他类型会报错 设置的时候是set test redis ex 200000等同于SETEX te ...
- 问题1-The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
问题一:The type java.lang.String cannot be resolved. It is indirectly referenced from required .class f ...