LeetCode Weekly Contest 19
504. Base 7
- User Accepted: 994
- User Tried: 1054
- Total Accepted: 1345
- Total Submissions: 3160
- Difficulty: Easy
Given an integer, return its base 7 string representation.
Example 1:
Input: 100 Output: "202"
Example 2:
Input: -7 Output: "-10"
Note: The input will be in range of [-1e7, 1e7].
简单的十进制转7进制。
class Solution { public: string convertToBase7(int num) { if(num == 0){ return "0"; } string s, p=""; if(num < 0){ p = "-"; num = -num; } while(num){ s = char('0' + (num%7)) + s; num = num/7; } return p+s; } };
513. Find Bottom Left Tree Value
- User Accepted: 870
- User Tried: 943
- Total Accepted: 1060
- Total Submissions: 2399
- Difficulty: Medium
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input: 2 / 1 3 Output: 1
Example 2:
Input: 1 / 2 3 / / 4 5 6 / 7 Output: 7
Note: You may assume the tree (i.e., the given root node) is not NULL.
树结构的迭代
/** * 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 ans, current; void Find(TreeNode *root, int row){ if(row >= current){ ans = root->val; current = row; } if(root->right != NULL){ Find(root->right, row+1); } if(root->left != NULL){ Find(root->left, row+1); } } int findBottomLeftValue(TreeNode* root) { ans = root->val; current = 1; Find(root, 1); return ans; } };
515. Find Largest Value in Each Tree Row
- User Accepted: 851
- User Tried: 878
- Total Accepted: 943
- Total Submissions: 2140
- Difficulty: Medium
You need to find the largest value in each row of a binary tree.
Example:
Input: 1 / 3 2 / 5 3 9 Output: [1, 3, 9]
也是用树结构,使用 BFS
/** * 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: vector<int> largestValues(TreeNode* root) { vector<int> ans; if(root == NULL){ return ans; } TreeNode *tmp; queue<TreeNode* > q; queue<int> qlevel; qlevel.push(1); q.push(root); int cur_level = 1, cur_val = -2147483648; while(!q.empty()){ tmp = q.front(); q.pop(); int tmp_level = qlevel.front(); qlevel.pop(); if(tmp_level == cur_level){ if(cur_val < tmp->val){ cur_val = tmp->val; } }else{ ans.push_back( cur_val ); cur_level = tmp_level; cur_val = tmp->val; } if(tmp->left != NULL){ q.push(tmp->left); qlevel.push(tmp_level + 1); } if(tmp->right != NULL){ q.push(tmp->right); qlevel.push(tmp_level + 1); } } ans.push_back(cur_val); return ans; } };
493. Reverse Pairs
- User Accepted: 165
- User Tried: 777
- Total Accepted: 172
- Total Submissions: 3595
- Difficulty: Hard
Given an array nums
, we call (i, j)
an important reverse pair if i < j
and nums[i] > 2*nums[j]
.
You need to return the number of important reverse pairs in the given array.
Example1:
Input: [1,3,2,3,1] Output: 2
Example2:
Input: [2,4,3,5,1] Output: 3
Note:
- The length of the given array will not exceed
50,000
. - All the numbers in the input array are in the range of 32-bit integer.
看了discuss 才有思路, 原来 merge sort 也有这么好功能
class Solution { private: int cnt; void countPair(vector<int>& nums, int lf, int md, int rg){ int l = lf, r = md + 1; while(l <= md && r <= rg){ if((long)nums[l] > (long)2*nums[r]){ cnt += (md - l + 1); r++; }else{ l++; } } sort(nums.begin() + lf, nums.begin() + rg + 1); } void Reverse(vector<int>& nums, int lf, int rg){ if(lf == rg){ return; } int mid = lf + (rg - lf)/2; Reverse(nums, lf, mid); Reverse(nums, mid+1, rg); countPair(nums, lf, mid, rg); } public: int reversePairs(vector<int>& nums) { int len = nums.size(); if(len <= 1){ return 0; } cnt = 0; Reverse(nums, 0, len-1); return cnt; } };