1. 程式人生 > >【LeetCode】周賽

【LeetCode】周賽

【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]

不會做。。。。。。。。。。。。。。