【LeetCode Weekly Contest 29】【2017/04/23】 第17周

  1. Binary Tree Tilt (3)
  2. Array Partition I (6)
  3. Longest Line of Consecutive One in Matrix (8)
  4. 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写一篇文章)的更多相关文章

  1. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

  2. 【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)

    注意,以前的比赛我是自己开了 virtual contest.这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力. 前面已经有了100个contest.计划是每周做三个到五个cont ...

  3. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

  4. 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

    Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...

  5. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  6. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  7. 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)

    Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...

  8. 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)

    Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...

  9. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...

随机推荐

  1. Callable使用示例

    之前工作中也有使用过Callable,但是却没有使用Thread对象去操作过,今晚 小组培训,涨知识了,现特意记录一下,以免忘记. 先看一下Thread的构造器 可以看到,Thread类并没有提供参数 ...

  2. linux常用基本命令 grep awk 待优化

    查看centos操作系统版本:cat /etc/centos-release 切换到当前用户主目录:cd 或者cd ~ 创建文件夹/a/b/c:mkdir -pv /a/b/c.如果/a/b/c的父目 ...

  3. vue项目适应不同屏幕做的适配器

    一般宽度是1920的,但是有的电脑屏幕很窄,导致页面样式错乱,那么可以设置app.vue以及主页面里的样式宽度为1920px,超过了就auto. 如下: (app.vue) (home.vue) 原效 ...

  4. 再探容斥好题——ROOK

    这个时候考过:安师大附中集训 Day2 当时看shadowice1984的做法,但是没有亲自写,,, 雅礼集训考试的时候鼓捣半天,被卡常到80pts,要跑9s 卡不动. 正解实际是: 3重容斥 1.随 ...

  5. 我们打开Podfile修改一下,以便将flutter包括在里面

    platform :ios, '9.0'target 'myproject' do end #新添加的代码flutter_application_path = '../'eval(File.read( ...

  6. ruby+selenium-webdriver测试

    参考这里的博客https://www.cnblogs.com/smiling007/p/5116662.html

  7. GARENA笔试sql20190926

    create database garena; use garena; create table players( account_id int, name varchar(20), country ...

  8. 爬虫之requests 请求

    1.发送不同的请求 import requests r = requests.get('https://www.baidu.com/') r = requests.post('http://httpb ...

  9. 12.定义Lock类,用于锁定数据.三步走,锁的优缺点

    #在threading模块当中定义了一个Lock类,可以方便的使用锁定: # #1.创建锁 # mutex = threading.Lock() # # #2.锁定 ''' mutex.acquire ...

  10. vuejs基础-常见指令(基本结构、v-cloak、v-text、v-html、v-bind、v-model\v-if、v-show)

    Vue之 - 基本的代码结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...