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 ...
随机推荐
- sqlserver查询分析器在本地服务器查看其它SqlServer服务器内容
exec sp_addlinkedserver 服务器自命名,'',sqloledb,要查询服务器的IP地址 exec sp_addlinkedsrvlogin 服务器自命名,false,null,账 ...
- dubbo之多协议
(1) 不同服务不同协议 比如:不同服务在性能上适用不同协议进行传输,比如大数据用短连接协议,小数据大并发用长连接协议 consumer.xml <?xml version="1.0& ...
- ROS:ubuntuKylin17.04-Ros使用OrbSLAM2
忙于图像处理和DCNN,很长时间不使用ROS,重新安装系统后,再次使用ORB-SLAM2(ROS)进行三维重建和实时追踪的演示. 参考以前的文章:ROS:ubuntu-Ros使用OrbSLAM ORB ...
- 金蝶WAFII
- eas之style接口
Obj可以是KDTable对象,也可以是IRow,IColumn,ICell对象锁定Obj.getStyleAttributes().setLocked(true);Obj.getStyleAttri ...
- js中二维数组的创建方法 2017-04-04 14:50 120人阅读 评论(0) 收藏
法一:var myarr=[[0,1,2],[1,2,3]]; 将[0,1,2]看做原来的0,将[1,2,3]看做原来的1,而二者又分别为子数组 如myarr[0][1]=1,myarr[1][1]= ...
- Asp.Net使用Yahoo.Yui.Compressor.dll压缩Js|Css
网上压缩css和js工具很多,但在我们的系统中总有特殊的地方.也许你会觉得用第三方的压缩工具很麻烦.我就遇到了这样问题,我不想在本地压缩,只想更新到服务器上去压缩,服务器压缩也不用备份之类的操作.于是 ...
- jetty+httpClient使用
背景: 看了https://www.cnblogs.com/donlianli/p/10954716.html这篇文章之后,突然发现自己的知识面太窄了,连这些几乎可以说基础的工具都没怎么用过,于是决定 ...
- UOJ #219 BZOJ 4650 luogu P1117 [NOI2016]优秀的拆分 (后缀数组、ST表)
连NOI Day1T1都不会做...看了题解都写不出来还要抄Claris的代码.. 题目链接: (luogu)https://www.luogu.org/problemnew/show/P1117 ( ...
- 嵌入式linux实现NAT端口映射
场景: 1.嵌入式linux系统内已经在2个网卡,分别为eth0(内网物理网卡,ip地址:192.168.1.4)以及ppp1(VPN客户端通过PPTP协议拨号生成的虚拟网卡,ip地址:192.168 ...