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 ...
随机推荐
- tomcat配置SSH加密
[root@tomcat2 ~]# keytool -genkeypair -alias tomcat -keyalg RSA -keystore /usr/local/tomcat7/keystor ...
- 关于大XML文件与大节点处理(System.Xml.XmlTextReader)
近期有个任务要求处理大XML文件,其中有个存了Base64的大节点(>90M,路径已知). 这种任务只能上XmlReader,即使如此大节点的处理还是头疼了一阵…… 最初查MSDN的时候,找到了 ...
- MySQL创建临时表
drop TEMPORARY table if EXISTS temp_table; create TEMPORARY table temp_table( id int not null, usern ...
- grunt入门 出处:http://artwl.cnblogs.com
grunt-contrib-uglify uglify是一个文件压缩插件,项目地址:https://github.com/gruntjs/grunt-contrib-uglify 本文将以一个DEMO ...
- code runner运行终端的目录设置
我的github:swarz,欢迎给老弟我++星星 该设置属性为 "code-runner.fileDirectoryAsCwd": true 设置为 true后,终端默认目录为运 ...
- CentOS安装新版RabbitMQ解决Erlang 19.3版本依赖
通过yum等软件仓库都可以直接安装RabbitMQ,但版本一般都较为保守. RabbitMQ官网提供了新版的rpm包(http://www.rabbitmq.com/download.html),但是 ...
- js动态渲染链接outline为随机颜色
[].forEach.call($("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.rando ...
- 47.serch基本语法
主要知识点 1._search api基本语法 2.http协议中get请求带上request body 一.search api的基本语法 1.GET /_search {所传递的参 ...
- lunix下的redis数据库操作——set集合
创建:(集合的特点是:有序,无重复) sadd set 1 2 3 4 5 6 查看: smembers set 删除元素: srem set 3 # 还剩 1 2 4 5 6 移动: sadd se ...
- hibernate4.3版本构造SessionFactory方法
hibernate3.X构造SessionFactory方法 //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().confi ...