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

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. Assembly之instruction之CMP

    CMP[.W]  Compare source and destinationCMP.B  Compare source and destination Syntax  CMP src,dst or ...

  2. 国外AI界牛人主页 及资源链接

    感觉 好博客要收集,还是贴在自己空间里难忘!!! 原文链接:http://blog.csdn.net/hitwengqi/article/details/7907366 http://people.c ...

  3. 初识cocos creator的一些问题

    本文的cocos creator版本为v1.9.01.color赋值cc.Label组件并没有颜色相关的属性,但是Node有color的属性. //如果4个参数,在ios下有问题let rgb = [ ...

  4. 关于大XML文件与大节点处理(System.Xml.XmlTextReader)

    近期有个任务要求处理大XML文件,其中有个存了Base64的大节点(>90M,路径已知). 这种任务只能上XmlReader,即使如此大节点的处理还是头疼了一阵…… 最初查MSDN的时候,找到了 ...

  5. 7.5 pragma 指令

    第1个pragma禁用了我们感兴趣的特别警告,第2个恢复了它.禁用尽可能小的一段代码的警告是一个良好的做法,以便你不会错过任何真正应该修复的错误.如果你想在单独一行上禁用或恢复多个警告,那么只需用逗号 ...

  6. 【JavaScript游戏开发】使用HTML5 canvas开发的网页版中国象棋项目

    //V1.0 : 实现棋子的布局,画布及游戏场景的初始化 //V2.0 : 实现棋子的颜色改变 //V3.0 :实现所有象棋的走棋规则 //V4.0 : 实现所有棋子的吃子功能 完整的项目源码已经开源 ...

  7. 单行函数、表连接(day02)

    回顾: 1.数据库介绍 sql: dql: select dml: insert delete update ddl: create drop alter tcl: commit rollback s ...

  8. while(Thread.activeCount() > 1)

    今天看到深入理解JVM第367页多线程volatile部分照着书本敲着代码发现了一个问题 Thread.activeCount()会一直大于2 public class VolatileTest { ...

  9. open-ldap schema (2)

    schema介绍及用途 schema 是OpenLDAP 软件的重要组成部分,主要用于控制目录树中各种条目所拥有的对象类以及各种属性的定义,并通过自身内部规范机制限定目录树条目所遵循的逻辑结构以及定义 ...

  10. CPA-IBE

    1.Transaction ID 生成机制 在有的情况下,我们需要得到固定格式的序列号,而不是数据库默认的自增序列号, 1.1 通常方式(隐式生成并通过触发器实时插入相关表) 例如我们要求此序列号必须 ...