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. 【汇编】dosbox钢琴

    DATA SEGMENT msg DB 0DH,0AH,'[ 1 2 3 4 5 6 7 ]' DB 0DH,0AH,' [ q w e r t y u ]' DB 0DH,0AH,'________ ...

  2. C# 从小到大排列

    "; ; var ss = ""; ;i<str.Length;i++) { var s0 = str[i].ToString(); var s1 = (js).T ...

  3. 测试模式 windows2008 内部版本7601

    win server 2008 r2 enterprise 64位系统. 最近手贱,对服务器进行了一下更新,结果傻叉了,这是什么鬼,明明显示已经激活的,但就是有这么一串碍眼的字幕. 电脑右下角居然出现 ...

  4. hadoop spark 总结

    yarn  由,资源管理器rm,应用管理器am appMaster,节点管理器nm 组成! 图侵删 yarn 的设计,是为了代替hadoop 1.x的jobtracker 集中式一对多的资源管理「资源 ...

  5. Java 各级注解及@Autowired注入为null解决办法

    1.@controller 控制器 用于标注控制层,相当于struts中的action层. 2.@service 服务层 用于标注服务层,主要用来进行业务的逻辑处理. 3.@repository DA ...

  6. py西游公关之模块

    Py西游攻关之模块   模块&包(* * * * *) 模块(modue)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可 ...

  7. PAT_A1153#Decode Registration Card of PAT

    Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...

  8. 慕课网页面app的滑动

    #coding=utf-8from appium import webdriver def get_driver(): desc={ "platformName":"An ...

  9. The story of one latency spike

    转自:https://blog.cloudflare.com/the-story-of-one-latency-spike/ A customer reported an unusual proble ...

  10. nyoj24-素数 距离问题

    素数距离问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数 ...