1.504. Base 7

水题,直接写。但是我错了一次,忘了处理0的情况。 没有考虑边界条件。写完代码,一般需要自己想几个边界测试用例进行测试。

 class Solution {
public:
string convertTo7(int num) {
if(num == ) return "";
int a = abs(num);
string res;
while(a) {
int t = a % ;
a /= ;
res += char(t + '');
}
if(num < )
res += "-";
reverse(res.begin(), res.end());
return res; }
};

上面的abs好像没考虑最大的负数溢出问题,注意!题目数据范围小!

2. 513. Find Left Most Element

水题吧,记录一下,每一层的起始元素,返回最后一层的起始值。

 /**
* 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 findLeftMostNode(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int res = root->val;
while(!q.empty()) {
int sz = q.size();
res = q.front()->val;
while(sz--) {
TreeNode* t = q.front();
q.pop();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return res;
}
};

3. 515. Find Largest Element in Each Row

跟第二题一样,也算水题吧,每一层求解一下最大值。

 /**
* 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> findValueMostElement(TreeNode* root) {
vector<int> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int sz = q.size();
int d = INT_MIN;
while(sz--) {
TreeNode* t = q.front();
q.pop();
d = max(d, t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
res.push_back(d);
}
return res;
}
};

4. 493. Reverse Pairs

原题,你一定做过求逆序对的题目,就跟这个一模一样。但是,我还是花了一些时间。依稀记得,这道题,我是原模原样见过,在上算法课的时候,老师讲divide and conquer的作业题,我记得我做错了,当时差不多搞懂了,现在又不会了!还是当初没搞懂啊!

一种解法是mergesort,在merge的时候,求解个数,求逆序对很容易计算,跟排序的循环一起,但是这道题目就不一样,需要单独的循环计算这个特别的逆序对。在开启一个循环计算,复杂对一样,只是增加了一些系数的时间。

另一种解法是树状数组,(当然,线段树也可以),按顺序插入一个数,记当前数为a[i],统计大于2*a[i]的元素的个数,然后插入a[i]. 插入和计数可以用树状数组高效的维护。需要注意(看的第一名大神的写法):树状数组的下标非负,这题数据有负数,还有数据范围很大,需要离散化处理,但是,又有二倍的数,不好处理,可以使用map,负数的下标加上一个base偏移来解决。 这题的数据很大,二倍的数据会溢出int,算是一个很大的坑吧! 题目做的多了,应该一眼就能看到这题的考点,数据溢出!看前面的神牛,就一次没有错!

贴上我写的代码,(写的不是很好,可以参考排名靠前大神们的代码):

mergesort:

 typedef long long ll;
class Solution {
public:
int res;
vector<int> b;
void mg(vector<int> &a, int x, int mid, int y) { int i = x, j = mid + ;
int p = x;
for (int k = ; k < y - x + ; k++) {
if(i > mid) {
b[p] = a[j];
j++; p++;
continue;
}
if(j > y) {
b[p] = a[i];
p++;
i++;
continue;
}
if(a[i] <= a[j]) {
b[p] = a[i];
i++; p++;
} else {
b[p] = a[j];
p++; j++;
}
}
i = x; j = mid + ;
for (int k = ; k < y - x + ; k++) {
if(i > mid) break;
if(j > y) break;
ll td = a[j];
if(a[i] > * td) {
res += (mid + - i);
j++;
} else i++;
}
for (int k = x; k <= y; k++)
a[k] = b[k];
}
void mergesort(vector<int> &a, int left, int right) {
if(left >= right) return;
int mid = (left + right) / ;
mergesort(a, left, mid);
mergesort(a, mid + , right);
mg(a, left, mid, right);
}
int reversePairs(vector<int>& nums) {
res = ;
int n = nums.size();
if(n < ) return res;
b = nums;
mergesort(nums, , n - );
return res;
}

树状数组:

 typedef long long ll;
class Solution {
public:
int res;
map<ll, int> ma;
ll base = 1ll << ;
ll lowbit(ll x) {
return x & -x;
}
void add(ll x) {
x += base;
while(x <= 1ll << ) {
ma[x]++;
x += lowbit(x);
}
}
int ask(ll x) {
x += base;
int res = ;
while(x) {
res += ma[x];
x -= lowbit(x);
}
return res;
}
int reversePairs2(vector<int>& nums) {
res = ;
int n = nums.size();
if(n < ) return res;
ma.clear();
for (int i = n - ; i >= ; i--) {
res += ask(nums[i] - 1ll);
add(2ll * nums[i]);
}
return res;
}

LeetCode Weekly Contest 19的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. 相机标定:PNP基于单应面解决多点透视问题

              利用二维视野内的图像,求出三维图像在场景中的位姿,这是一个三维透视投影的反向求解问题.常用方法是PNP方法,需要已知三维点集的原始模型. 本文做了大量修改,如有不适,请移步原文:  ...

  2. 【sqli-labs】 less40 GET -Blind based -String -Stacked(GET型基于盲注的堆叠查询字符型注入)

    提交,页面正常,说明是')闭合的 http://192.168.136.128/sqli-labs-master/Less-40/?id=1')%23 http://192.168.136.128/s ...

  3. (转)Bootstrap 之 Metronic 模板的学习之路 - (2)源码分析之 head 部分

    https://segmentfault.com/a/1190000006684122 下面,我们找个目录里面想对较小的文件来分析一下源码结构,我们可以看到,page_general_help.htm ...

  4. eas之设定table选择模式

    tblMain.getSelectManager().setSelectMode(0);--不能选择  tblMain.getSelectManager().setSelectMode(1);--选择 ...

  5. 爬虫系列(十二) selenium的基本使用

    一.selenium 简介 随着网络技术的发展,目前大部分网站都采用动态加载技术,常见的有 JavaScript 动态渲染和 Ajax 动态加载 对于爬取这些网站,一般有两种思路: 分析 Ajax 请 ...

  6. Yii2验证码使用教程

    控制器代码 public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaActio ...

  7. vue cli 平稳升级webapck4

    webpack4 released 已经有一段时间了,插件系统趋于平稳,适逢对webpack3的打包速度很不满意,因此决定将当前在做的项目进行升级,正好也实践一下webpack4. 新特性 0配置 应 ...

  8. VS2015 C++ 获取 Edit Control 控件的文本内容,以及把获取到的CString类型的内容转换为 int 型

    UpdateData(true); //读取编辑框内容,只要建立好控件变量后调用这个函数使能,系统就会自动把内容存在变量里 //这里我给 Edit Control 控件创建了一个CString类型.V ...

  9. 【codeforces 797D】Broken BST

    [题目链接]:http://codeforces.com/contest/797/problem/D [题意] 给你一个二叉树; 然后问你,对于二叉树中每个节点的权值; 如果尝试用BST的方法去找; ...

  10. ZOJ - 3483 - Gaussian Prime

    先上题目: Gaussian Prime Time Limit: 3 Seconds      Memory Limit: 65536 KB In number theory, a Gaussian ...