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 ...
随机推荐
- Window8.1下安装Matplotlib库
有两种方法: 直接选用一些预打包库软件,如WinPython, Python(x,y), Enthought Canopy, or Continuum Anaconda.这些软件中已包含有Matplo ...
- 团体程序设计天梯赛-练习集-L1-041. 寻找250
L1-041. 寻找250 对方不想和你说话,并向你扔了一串数…… 而你必须从这一串数字中找到“250”这个高大上的感人数字. 输入格式: 输入在一行中给出不知道多少个绝对值不超过1000的整数,其中 ...
- C#使用OracleDataReader返回DataTable
string data = string.Empty; DataTable OutDataTable = new DataTable(); OracleDataReader daReader = cm ...
- TCP中的RST标志(Reset)详解
在谈RST攻击前,必须先了解TCP:如何通过三次握手建立TCP连接.四次握手怎样把全双工的连接关闭掉.滑动窗口是怎么传输数据的.TCP的flag标志位里RST在哪些情况下出现.下面我会画一些尽量简化的 ...
- jmeter3.1 压测
压测目标:error 为0,线程起到250,服务器配置达到最大 一.Jmeter3.1 压测 JMeter3.1提供一个用于生成HTML页面格式图形化报告的扩展模块.该模块支持通过两种方式生成多维度图 ...
- JDBC对MySQL数据库存储过程的调用
一.MySQL数据库存储过程: 1.什么是存储过程 存储过程(英文:Stored Procedure)是在大型数据库系统中,为了完成特定功能而编写的一组的SQL语句集.存储过程经编译存储在数据库中,用 ...
- selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)
跟你说,你总是靠那个firebug,chrome的F12啥的右击复制xpath绝对总有一天踩着地雷炸的你死活定位不到,这个时候就需要自己学会动手写xpath,人脑总比电脑聪明,开始把xpath语法给我 ...
- LA 3363
Run Length Encoding(RLE) is a simple form of compression. RLE consists of the process for searching ...
- CentOS 7.2 x64 配置SVN服务器
说明: SVN(subversion)的运行方式有两种: 一种是基于Apache的http.https网页访问形式,还有一种是基于svnserve的独立服务器模式. SVN的数据存储方式也有两种:一种 ...
- Django admin(四)一些有用定制
原文:https://www.cnblogs.com/linxiyue/p/4075048.html Model实例,myapp/models.py: 1 2 3 4 5 6 7 8 9 10 11 ...