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. TCP协议的三次握手、四次挥手

    TCP三次握手 TCP的连接的建立需要发送三个包,一次称为三次握手(Three-way Handshake). 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方的序列号和确认号并交换 ...

  2. hadoop 安装问题总结

    安装启动步骤  [英语好的,直接手把手跟着来] http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/Sing ...

  3. HDU 2266 How Many Equations Can You Find(模拟,深搜)

    题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string ...

  4. [luogu1155 NOIP2008] 双栈排序 (二分图染色)

    传送门 Description Input 第一行是一个整数 n . 第二行有 n 个用空格隔开的正整数,构成一个 1−n 的排列. Output 共一行,如果输入的排列不是"可双栈排序排列 ...

  5. vue中访问数据接口的配置

    业务API接口地址: http://localhost:3816/api/ 前端UI浏览地址:http://127.0.0.1:8080/#/home 由于同源策略的问题: 需要配置代理: 在开发环境 ...

  6. 2.2 SVN的简单使用

    1.打开SVN服务器 选中Repositories→右键→Create new Repositories 选中Test2→右键→Copy URL to Clipboard 打开记事本粘贴地址:http ...

  7. lucene_09_solrj的使用

    什么是solrj solrj 是访问Solr 服务的java客户端,提供索引(增删改)和搜索(查)的请求方法,Solrj 通常在嵌入在业务系统中,通过Solrj的API接口操作Solr服务,如下图: ...

  8. (25)Spring Boot使用自定义的properties【从零开始学Spring Boot】

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 若继续在application.properties中添加 如: ...

  9. 用DIME格式来组织自定义格式

    直接网际消息封装(Direct Internet Message Encapsulation,即DIME)格式提供了一种简单而又标准的机制,这个机制可以把多文本(multiple text)和二进制数 ...

  10. [Drupal]主题教程

    drupal6和drupal7的主题开发有很大不同,本指南包含了这些不同 drupal7的默认主题是Bartik,6的是Garland drupal的主题系统是如何工作的 这部分内容主要讲述的是dru ...