LeetCode Weekly Contest 24
1, 543. Diameter of Binary Tree
维护左右子树的高度,同时更新结果,返回以该节点结束的最大长度。递归调用。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int res;
int work(TreeNode* root) {
if(!root) return ;
int a = work(root->left), b = work(root->right);
res = max(res, a + b + );
return max(a, b) + ;
}
int diameterOfBinaryTree(TreeNode* root) {
res = ;
work(root);
if(res >= ) res -= ;
return res;
}
};
2. 538. Convert BST to Greater Tree
利用bst的有序性,一次遍历,同时维护后缀和,更新节点。递归调用。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int s;
void work(TreeNode* root) {
if(!root) return;
if(!root->left && !root->right) {
root->val += s;
s = root->val;
return;
}
if(root->right) work(root->right);
root->val += s;
s = root->val;
work(root->left);
}
TreeNode* convertBST(TreeNode* root) {
s = ;
work(root);
return root;
}
};
3. 542. 01 Matrix
简单的bfs,初始为0的节点加入队列,然后更新相邻节点,直到最后的结果为空。
int dx[] = {, -, , };
int dy[] = {, , , -};
class Solution {
public:
int n, m;
bool check(int x, int y) {
if(x >= && x < n && y >= && y < m) return ;
return ;
}
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
n = matrix.size();
m = matrix[].size();
typedef pair<int, int> pii;
vector<vector<int>> res(n, vector<int>(m, INT_MAX));
queue<pii> q;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if(matrix[i][j] == ) {
res[i][j] = ;
q.push({i, j});
}
}
}
int x, y, v;
while(!q.empty()) {
x = q.front().first, y = q.front().second;
q.pop();
v = res[x][y];
for (int i = ; i < ; i++) {
int cx = x + dx[i], cy = y + dy[i];
if(check(cx, cy)) {
if(res[cx][cy] > v + ) {
res[cx][cy] = v + ;
q.push({cx, cy});
}
}
}
}
return res;
}
};
4. 544. Output Contest Matches
一个队的rank在整个过程是不变的,都是第一个元素决定的,然后每次生成新节点,维护下顺序,第一个和最后一个合并,直到只剩一个为止。
class Solution {
public:
string findContestMatch(int n) {
vector<string> a;
for (int i = ; i < n / ; i++) {
stringstream ss;
ss << "(" << i + << "," << (n - i) << ")";
a.push_back(ss.str());
}
vector<string> b;
int sz = a.size();
while(a.size() > ) {
b.clear();
sz = a.size();
for (int i = ; i < sz / ; i++) {
string t = "(" + a[i] + "," + a[sz - i - ] + ")";
b.push_back(t);
}
swap(a, b);
}
return a[];
}
};
LeetCode Weekly Contest 24的更多相关文章
- 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 ...
随机推荐
- (转)基于Metronic的Bootstrap开发框架经验总结(4)--Bootstrap图标的提取和利用
http://www.cnblogs.com/wuhuacong/p/4762924.html 在前面的一篇随笔<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模 ...
- UNIX SOCKET编程简介
1 . Layered Model of Networking Socket 编程的层次模型如下图所示, 最上面是应用层,应用层下面的是 SOCKET API 层,再下面是传输层和网络层 ...
- 常用的 CSS 技巧
1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate { filter: grayscale(%); -webkit-filter: grayscale( ...
- mount 命令总结
配置CnetOS 7.4 本地yum源,记录下遇到的ISO镜像挂载问题,使用 blkid 命令可以查看设备的UUID.Label.文件系统类型(iso镜像文件系统类型iso9660) [root@lo ...
- 多文件编程(day13)
多文件编程时一个文件里可以包含多个函数, 一个函数只能属于一个文件 多文件编程的步骤 .把所有函数分散在多个不同的源文件里 (主函数通常单独占一个文件) .为每个源文件编写一个配对的以.h作为 扩展名 ...
- linux配置Jdk1.8
Jdk1.8:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html cd /usr/ ...
- 将项目上传到Github之使用git命令上传
1,先从GitHub网页上建立一个数据仓库 2,安装git 下载地址:https://www.git-scm.com/download/win 3,找到本地要上传的项目目录,右键点击Git Bash ...
- Centos7.5虚拟机无法ping通网关、外网IP地址
问题:前两天Centos7.5虚拟机关机,第二天重启后使用Xshell发现无法连接虚拟机,经检测发现虚拟机无法ping通192.168.1.1.无法ping通192.168.1.118(客户机)和ww ...
- javascript中创建新节点的方法 标签: javascript 2016-12-25 11:38 55人阅读 评论(0)
一. var newnode=document.createElement("i"); var newnodeText=document.createTextNode(" ...
- python数据标准化
def datastandard(): from sklearn import preprocessing import numpy as np x = np.array([ [ 1., -1., 2 ...