LeetCode Weekly Contest 21
1. 530. Minimum Absolute Difference in BST
最小的差一定发生在有序数组的相邻两个数之间,所以对每一个数,找他的前驱和后继,更新结果即可!再仔细一想,bst的中序遍历就是有序数组,每次记录上一个数字,求他们的差,更新结果即可!
class Solution {
public:
int res;
int x;
void work(TreeNode* root) {
if(!root) return;
if(root->left) work(root->left);
if(x != -) {
res = min(res, abs(x - root->val));
}
x = root->val;
if(root->right) work(root->right);
}
int getMinimumDifference(TreeNode* root) {
res = INT_MAX;
x = -;
work(root);
return res;
}
};
2. 523. Continuous Subarray Sum
这题,我太坑了,错了4次,导致罚时非常多!
一看这题,不很简单么!1. 求连续的和为某一个数,用前缀和处理一下就好了, 2. 求是k的倍数,倍数很多怎么办,转化为求余数即可。(然后你就注意到:如果k=0,怎么解决,这是个大坑啊!)。
这题一定注意,长度要大于等于2啊!时刻牢记!
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size();
if(n < ) return ;
if(k == ) {
for (int i = ; i < n - ; i++) {
if(nums[i] + nums[i + ] == ) return ;
}
return ;
}
int s = ;
set<int> se;
int c = ;
for (int x : nums) {
s += x;
c++;
s %= k;
if(s == && c > ) return ;
if(se.count(s)) return ;
se.insert(s);
}
return ;
}
};
我这个代码好像还不对,没有判读长度为2的情况!会不会重判,上面的代码,显然是错误的!
采用map记录一下下标!
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size();
if(n < ) return ;
if(k == ) {
for (int i = ; i < n - ; i++) {
if(nums[i] + nums[i + ] == ) return ;
}
return ;
}
int s = ;
set<int> se;
map<int, int> ma;
int c = ;
for (int x : nums) {
s += x;
c++;
s %= k;
if(s == && c > ) return ;
if(se.count(s) && (c - ma[s]) >= ) return ;
se.insert(s);
ma[s] = c;
}
return ;
}
};
3. 524. Longest Word in Dictionary through Deleting
这题就是暴力,先对字典排序,然后逐个比较,返回。
class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
int n = s.size();
if(n == ) return s;
int m = d.size();
if(m == ) return "";
sort(d.begin(), d.end(), [&](string x, string y) {
if(x.size() == y.size()) return x < y;
return x.size() > y.size();
});
for (string x : d) {
if(x.size() > n) continue;
int i, j;
i = j = ;
int len = x.size();
while(i < n && j < len) {
if(s[i] == x[j]) {
i++; j++;
} else i++;
}
if(j == len) {
return x;
}
}
return "";
}
};
4. 529. Minesweeper
我就想,这次的最后一题分值有点小啊!原来是medium的!
理解题意,考虑各种情况,然后就是做一个bfs,记住判重和边界条件。
class Solution {
public:
int n, m;
bool check(int x, int y) {
if(x >= && x < n && y >= && y < m) return ;
return ;
}
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
n = board.size(), m = board[].size();
int x = click[], y = click[];
if(board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}
int s = ;
for (int i = -; i <= ; i++) {
for (int j = -; j <= ; j++) {
int cx = x + i, cy = y + j;
if(!check(cx, cy)) continue;
s += board[cx][cy] == 'M';
}
}
if(s != ) {
board[x][y] = '' + s;
return board;
}
vector<vector<bool>> in(n, vector<bool>(m, ));
queue<pair<int, int>> q;
q.push({x, y});
while(!q.empty()) {
auto t = q.front(); q.pop();
x = t.first, y = t.second;
s = ;
for (int i = -; i <= ; i++) {
for (int j = -; j <= ; j++) {
int cx = x + i, cy = y + j;
if(!check(cx, cy)) continue;
s += board[cx][cy] == 'M';
}
}
if(s == ) {
board[x][y] = 'B';
for (int i = -; i <= ; i++) {
for (int j = -; j <= ; j++) {
int cx = x + i, cy = y + j;
if(!check(cx, cy)) continue;
if(board[cx][cy] == 'E' && !in[cx][cy]) {
q.push({cx, cy});
in[cx][cy] = ;
}
}
}
} else {
board[x][y] = '' + s;
}
}
return board;
}
};
LeetCode Weekly Contest 21的更多相关文章
- 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 ...
随机推荐
- OpenCV视频进度播放控制
本来打算把进度条嵌入MFC的PIC空间里面,结果显示进度条消失,看来还是不要这个样子了. 全局变量区域: //2.初始化进度条的位置 int G_slider_position = 0; CvCapt ...
- 实验8 标准模板库STL
一.实验目的与要求: 了解标准模板库STL中的容器.迭代器.函数对象和算法等基本概念. 掌握STL,并能应用STL解决实际问题. 二.实验过程: 完成实验8标准模板库STL中练习题,见:http:// ...
- javaee 用Buffered进行对象的写入和读取
package Zjshuchu; import java.io.Serializable; public class Dog implements Serializable{ private ...
- 安卓 九宫格 GridView 的表格布局
首先,请大家理解一下“迭代显示”这个概念,这个好比布局嵌套,我们在一个大布局里面重复的放入一些布局相同的小布局,那些重复的部分是由图片和文字组成的小控件,图片在上方,文字在下方,之后我们只需要把这些小 ...
- 图的BFS
目录: 一.算法的基本思路 二.算法过程 三.题目:785判断是否为二分图 https://blog.csdn.net/weixin_40953222/article/details/80544928 ...
- 3. IDEA 的样式设置和快捷键设置
一.样式设置 首先打开IDEA之后,点击任务栏的“File”→Settings 二.设置快捷键 1.首先打开IDEA之后,点击任务栏的“File”. 2.在下拉列表中中选择“Settings” 3.在 ...
- spring-boot-starter-actuator监控接口详解
spring-boot-starter-actuator 是什么 一句话,actuator是监控系统健康情况的工具. - 怎么用? 1. 添加 POM依赖 <dependency> < ...
- Spring深入理解(二)
这个方法实现了 AbstractApplicationContext 的抽象方法 refreshBeanFactory,这段代码清楚的说明了 BeanFactory 的创建过程.注意 BeanFact ...
- TensorFlow 便捷的实现机器学习 三
TensorFlow 便捷的实现机器学习 三 MNIST 卷积神经网络 Fly Overview Enabling Logging with TensorFlow Configuring a Vali ...
- [Javascript Crocks] Recover from a Nothing with the `alt` method
Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Mayb ...