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. javaee字符文件的复制

    package Zy; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWrit ...

  2. BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...

  3. BZOJ 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 贪心 + 问题转化

    Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the ...

  4. 复习MySQL③导入数据、检查及修改

    导入数据: 用insert into语句为表插入数据: - insert into 表名(字段1,字段2,…) values …… 导入外部文本文件: - 导入外部txt文件(导入CSV文件分隔符为' ...

  5. 记录:Ubuntu下升级Python从2.x到3.x

    一.安装Python3 在Ubuntu中的终端输入:sudo apt-get install python3 提示资源被锁住,可能有另外一个程序在占用此资源. 解决方法:输入以下指令解锁资源 sudo ...

  6. PAT_A1098#Insertion or Heap Sort

    Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort  ...

  7. C#第十六节课

    out using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.T ...

  8. 使用for或while循环来处理处理不确定页数的网页数据爬取

    本文转载自以下网站: Python For 和 While 循环爬取不确定页数的网页  https://www.makcyun.top/web_scraping_withpython16.html 需 ...

  9. SQLServer · BUG分析 · Agent 链接泄露分析(转载)

    背景 SQLServer Agent作为Windows服务提供给用户定期执行管理任务,这些任务被称为Job:考虑应用镜像的场景如何解决Job同步问题,AWS RDS的做法是不予理会,由用户维护Job, ...

  10. DateTime日期格式转换,不受系统格式的影响

    Application.Initialize;  with FormatSettings do  begin    ShortDateFormat := 'yyyy-mm-dd';    LongDa ...