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

  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. Java_Web之宠物管理系统

    使用JSP+servLet实现宠物管理系统,oraC1e11g作为后台数据厍,实现查看宠物和增加宠物的功能由你实现,如图: 其中宠物包栝:狗.猫.鸟.鼠 具体要求及推荐实现步骤 第一步:创建数据库代码 ...

  2. Java_Web之神奇的Ajax

    为什么使用Ajax? 无刷新:不刷新整个页面,只刷新局部 无刷新的好处 提供类似C/S的交互效果,操作更方面 只更新部分页面,有效利用带宽   什么是Ajax?   XMLHttpRequest常用方 ...

  3. 三维重建5:场景中语义分析/语义SLAM/DCNN-大尺度SLAM

    前言: 在实时/非实时大规模三维场景重建中,引入了语义SLAM这个概念,参考三维重建:SLAM的尺度和方法论问题和三维重建:SLAM的粒度和工程化问题 .大规模三维场景重建的尺度增大,因此相对于整个重 ...

  4. QT4使用HDF5 类型错误

    使用HDF5 :HDF5_1.10.0 出现: fatal error C1083: 无法打开包括文件:"stdbool.h": No such file or directory ...

  5. nginx_安装测试

    首先安装环境: [root@local nginx-1.9.14]#  yum install gcc-c++  pcre pcre-devel  zlib zlib-devel openssl op ...

  6. mui scrollTo到指定位置,出现空白页及拉不动的问题解决

    使用方式简介 mui 列表页使用的是 mui的插件实现的上拉加载下拉刷新,但是从详情页回到列表页时 不能回到之前的位置.所以想到了使用缓存. 第一次和第二次的试验是失败的.失败后,就想用其他办法来解决 ...

  7. Android内存优化————加载长图

    项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...

  8. vmware Horizon 7 与远程桌面(mstsc)兼容性问题解决办法

    关于Horizon 7 Agent与远程桌面(mstsc)兼容性问题解决办法 在Horizon 7环境中,在桌面模板安装了Horizon Agent后,就无法直接通过微软的远程桌面(mstsc)工具连 ...

  9. DOCKER - J2EE中容器:WEB容器、EJB容器

    转自:http://www.voidcn.com/article/p-yizkqdxp-zg.html

  10. eas之获取当前登陆信息

    public void getSystemInfo()    {        // SysContext工具类可获取当前登陆用户的信息,可根据需要进行调用.        // 举两例如下:     ...