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

  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. Postgresql_最新版11.2源码编译安装

    pg官网:https://www.postgresql.org/ yum -y gcc gcc-c++ cmake ncurses-devel perl zlib* .去官网下载源码包. 下载地址:h ...

  2. S-HR薪酬项目与核算表的关系

  3. 定位IO瓶颈的方法,iowait低,IO就没有到瓶颈?

    通过分析mpstat的iowait和iostat的util%,判断IO瓶颈 IO瓶颈往往是我们可能会忽略的地方(我们常会看top.free.netstat等等,但经常会忽略IO的负载情况),今天给大家 ...

  4. 递归、作用域、生命周期(day09)

    数组可以作为形式参数使用 数组做形式参数的时候真正的形式参数并 不是数组而是一个可以作为数组使用的 变量 数组形式参数里包含的存储区都不是被调用 函数提供的 声明数组形式参数的时候可以省略中括号 里的 ...

  5. 布尔类型、操作符别名、C++函数、动态内存分配(new\delete)、引用(day02)

    六 C++的布尔类型 bool类型是C++中基本类型,专门表示逻辑值:true/false bool在内存上占一个字节:1表示true,0表示false bool类型可以接收任意类型和表达式的结果,其 ...

  6. Linux思维导图之inode、mv、cp和硬软链接

    标准I / O和管道:     ps aux进程管理命令(和win任务管理器一样);     当前命令行输出窗口,键盘的输入即是标准输入.标准输出就是执行了的命令,无法执行的命令或错误信息是标准错误, ...

  7. 支持移动触摸的jQuery图片Lightbox插件

    简介 这是一款支持移动触摸设备的简洁jQuery图片Lightbox插件.该LightBox插件可以在移动手机和桌面设备中运行,它具有响应式,预加载图片,键盘支持等特点,非常实用.它的特点还有: 响应 ...

  8. Django admin(四)一些有用定制

    原文:https://www.cnblogs.com/linxiyue/p/4075048.html Model实例,myapp/models.py: 1 2 3 4 5 6 7 8 9 10 11 ...

  9. STM32 HAL库的定时器中断回调函数跟串口中断回调函数

    void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { //添加回调后的程序逻辑 if (htim->Instance == ...

  10. MongoDB简介、特点、原理、使用场景、应用案例

    简介 MongoDB[1] 是一个基于分布式文件存储的数据库.由C 语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB[2] 是一个介于关系数据库和非关系数据库之间的产品, ...