【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)
【LeetCode Weekly Contest 29】【2017/04/23】 第17周
- Binary Tree Tilt (3)
- Array Partition I (6)
- Longest Line of Consecutive One in Matrix (8)
- Find the Closest Palindrome (10)
第一题: 563. Binary Tree Tilt [easy]
别人写几行,我写的非常啰嗦==。树后序遍历。代码是重写过的。
Given a binary tree, return the tilt of the whole tree.
The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.
The tilt of the whole tree is defined as the sum of all nodes' tilt.
Example:
Input:
1
/ \
2 3
Output: 1
Explanation:
Tilt of node 2 : 0
Tilt of node 3 : 0
Tilt of node 1 : |2-3| = 1
Tilt of binary tree : 0 + 0 + 1 = 1
/**
* 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 findTilt(TreeNode* root) {
int ans = ;
dfs(root, ans);
return ans;
}
int dfs(TreeNode* root, int& ans) {
if (!root) return ;
int l = dfs(root->left, ans);
int r = dfs(root->right, ans);
root->val = root->val + l + r;
ans += abs(l-r);
return root->val;
}
};
第二题: 561. Array Partition I [easy]
给2N个数,两两配对之后,使得每对最小值的和加起来最大。sort一下,随便搞搞。
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
if (nums.empty()) return ;
sort(nums.begin(), nums.end());
int ans = ;
for(int i = ; i < nums.size(); i += ) {
ans += nums[i];
}
return ans;
}
};
第三题: 562. Longest Line of Consecutive One in Matrix [Medium]
给一个01矩阵, 横,竖,对角线,和反对角线找连续1的最长一条线。搜索。
【别人都写四个方向,结果我写了八个方向还想着去剪枝orz】
class Solution {
public:
vector<pair<int, int>> dir{{, }, {, }, {, }, {-, }};
int longestLine(vector<vector<int>>& M) {
if (M.size() == || M[].size() == ) { return ; }
int ans = ;
dfs(M, ans);
return ans;
}
void dfs (vector<vector<int>>& M, int& ans) {
for (int i = ; i < M.size(); ++i) {
for (int j = ; j < M[].size(); ++j) {
if (M[i][j] == ) {
for(int idx = ; idx < ; ++idx) {
int length = ;
go(M, i, j, idx, length);
ans = max(ans, length);
}
}
}
}
}
bool valid (vector<vector<int>>& M, int newI, int newJ) {
if (newI >= && newI < M.size() && newJ >= && newJ < M[].size()) {
return true;
}
return false;
}
void go(vector<vector<int>>& M, int I, int J, const int idx, int& length) {
int newI = I + dir[idx].first, newJ = J + dir[idx].second;
if (valid(M, newI, newJ) && M[newI][newJ]) {
length++;
go(M, newI, newJ, idx, length);
}
return;
}
};
第四题: 564. Find the Closest Palindrome [Hard]
不会做。。。。。。。。。。。。。。
【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)的更多相关文章
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
- 【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)
注意,以前的比赛我是自己开了 virtual contest.这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力. 前面已经有了100个contest.计划是每周做三个到五个cont ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
- 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)
Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)
Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...
- 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)
Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...
- LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...
随机推荐
- 洛谷 P4827 [国家集训队] Crash 的文明世界
题目描述 给你一棵 n 个点的树,对于树上的每个节点 i,求 \(\sum_{j=1}^ndis(i,j)^k\).其中 \(dis(i,j)\) 为两点在树上的距离. 输入格式 第一行两个整 ...
- ajax传递对象到MVC控制器
1.view层中ajax写法: function Add2() { var model = new Object(); model.UserName = $('#UserName').val(); m ...
- mysql错误日志及sql日志的区别
my.ini # power by phpStudy 2014 www.phpStudy.net 官网下载最新版 [client] port=3306 [mysql] default-characte ...
- linux中awk 详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报表生 ...
- loj#2334 「JOI 2017 Final」JOIOI 王国
分析 二分答案 判断左上角是否满足 为了覆盖所有范围 我们依次把右下角,左上角,右上角移动到左上角 代码 #include<bits/stdc++.h> using namespace s ...
- CycleGAN --- Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks
文章地址:http://openaccess.thecvf.com/content_ICCV_2017/papers/Zhu_Unpaired_Image-To-Image_Translation_I ...
- Tomcat服务器时间不正确
================================1=============================== 增加Tomcat参数设置"-Duser.timezone=G ...
- Vagrant 手册之 Vagrantfile - SSH 设置 config.ssh
原文地址 配置的命名空间:config.ssh config.ssh 中的设置与配置 Vagrant 如何通过 SSH 访问您的计算机相关. 大多数 Vagrant 设置一样,一般使用默认设置即可,但 ...
- EasyUI的datagrid列属性添加超链接
$("#dg").datagrid({ url: "../Ajax/PurchaseAjax.ashx", queryParams: ...
- Caused by: java.lang.ClassNotFoundException: com.alibaba.dubbo.common.Version
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-bo ...