zoukankan      html  css  js  c++  java
  • 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)

    【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
     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     int findTilt(TreeNode* root) {
    13         int ans = 0;
    14         dfs(root, ans);
    15         return ans;
    16     }
    17     int dfs(TreeNode* root, int& ans) {
    18         if (!root) return 0;
    19         int l = dfs(root->left, ans);
    20         int r = dfs(root->right, ans);
    21         root->val = root->val + l + r;
    22         ans += abs(l-r);
    23         return root->val;
    24     }
    25 };
    View Code

    第二题: 561. Array Partition I [easy]

    给2N个数,两两配对之后,使得每对最小值的和加起来最大。sort一下,随便搞搞。

     1 class Solution {
     2 public:
     3     int arrayPairSum(vector<int>& nums) {
     4         if (nums.empty()) return 0;
     5         sort(nums.begin(), nums.end());
     6         int ans = 0;
     7         for(int i = 0; i < nums.size(); i += 2) {
     8             ans += nums[i];
     9         }
    10         return ans;
    11     }
    12 };
    View Code

    第三题: 562. Longest Line of Consecutive One in Matrix [Medium]

    给一个01矩阵, 横,竖,对角线,和反对角线找连续1的最长一条线。搜索。

    【别人都写四个方向,结果我写了八个方向还想着去剪枝orz】

     1 class Solution {
     2 public:
     3     vector<pair<int, int>> dir{{1, 0}, {1, 1}, {0, 1}, {-1, 1}};
     4     int longestLine(vector<vector<int>>& M) {
     5         if (M.size() == 0 || M[0].size() == 0) { return 0; }
     6         int ans = 0;
     7         dfs(M, ans);
     8         return ans;
     9     }
    10     void dfs (vector<vector<int>>& M, int& ans) {
    11         for (int i = 0; i < M.size(); ++i) {
    12             for (int j = 0; j < M[0].size(); ++j) {
    13                 if (M[i][j] == 1) {
    14                     for(int idx = 0; idx < 4; ++idx) {
    15                         int length = 1;
    16                         go(M, i, j, idx, length);
    17                         ans = max(ans, length);
    18                     }
    19                 }
    20             }
    21         }
    22     }
    23     
    24     bool valid (vector<vector<int>>& M, int newI, int newJ) {
    25         if (newI >= 0 && newI < M.size() && newJ >= 0 && newJ < M[0].size()) {
    26             return true;
    27         }
    28         return false;
    29     }
    30     
    31     void go(vector<vector<int>>& M, int I, int J, const int idx, int& length) {
    32         int newI = I + dir[idx].first, newJ = J + dir[idx].second;
    33         if (valid(M, newI, newJ) && M[newI][newJ]) {
    34             length++;
    35             go(M, newI, newJ, idx, length);
    36         }
    37         return;
    38     }
    39 };
    View Code

    第四题: 564. Find the Closest Palindrome [Hard]

    不会做。。。。。。。。。。。。。。

  • 相关阅读:
    Python 高级编程系列__03:python 中常见的内置类型
    Python 高级编程系列__02:type、object、class 的关系
    Python 高级编程系列__01:一切皆对象
    Mac 修改默认python的版本
    swap指令实现互斥
    什么是进程同步?wait( )是如何实现进程同步的?
    可执行文件加载时进行了哪些处理?
    C++不允许使用不完整的类型说明
    error LNK2019: 无法解析的外部符号
    抽屉原理——放苹果
  • 原文地址:https://www.cnblogs.com/zhangwanying/p/6753040.html
Copyright © 2011-2022 走看看