【LeetCode】Missing Ranges
Missing Ranges
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges.
For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].
逐个数组元素遍历,将间隙取出即可。注意两边的情况。
class Solution {
public:
vector<string> findMissingRanges(int A[], int n, int lower, int upper)
{
vector<string> ret;
if(n == )
return ret;
string str;
if(lower < A[])
{
if(lower < A[]-)
str = to_string((long long)lower) + "->" + to_string((long long)A[]-);
else
//lower == A[0]-1
str = to_string((long long)lower);
ret.push_back(str);
}
for(int i = ; i < n; i ++)
{
if(A[i] - A[i-] < )
continue;
if(A[i] - A[i-] > )
str = to_string((long long)A[i-]+) + "->" + to_string((long long)A[i]-);
else
//A[i] - A[i-1] == 2
str = to_string((long long)A[i-]+);
ret.push_back(str);
}
if(A[n-] < upper)
{
if(A[n-] < upper-)
str = to_string((long long)A[n-]+) + "->" + to_string((long long)upper);
else
//upper == A[n-1]+1
str = to_string((long long)upper);
ret.push_back(str);
}
return ret;
}
};
以下是我的测试用例,全部通过:
void display(vector<string>& ret)
{
for(int i = ; i < ret.size(); i ++)
{
cout << ret[i] << " ";
}
cout << endl;
} int main()
{
Solution s;
int A[];
//expect: ["0->9"]
vector<string> ret = s.findMissingRanges(A, , , );
display(ret); int B[] = {};
//expect: ["0->5", "7->9"]
ret = s.findMissingRanges(B, , , );
display(ret); int C[] = {, , };
//expect: []
ret = s.findMissingRanges(C, , , );
display(ret); int D[] = {, , , , };
//expect: ["2", "4->49", "51->74", "76->99"]
ret = s.findMissingRanges(D, , , );
display(ret);
}
【LeetCode】Missing Ranges的更多相关文章
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- uva 10712 - Count the Numbers(数位dp)
题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...
- Spark:JavaRDD 转化为 Dataset<Row>的两种方案
JavaRDD 转化为 Dataset<Row>方案一: 实体类作为schema定义规范,使用反射,实现JavaRDD转化为Dataset<Row> Student.java实 ...
- GIT 如何从另一分支合并特定的文件
是否遇到过这种情景: 您在一个分支上工作,发现该分支上的某些文件实现的功能已经在其他分支上实现了 但因为这两个分支实现不同的功能,因此不能进行简单的合并工作,但您又不想重复其他已经完成的工作 以下操作 ...
- 最小公倍数 SRM 661 Div1 250: MissingLCM
Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...
- [Canvas]游戏增加怪物爆炸效果,改善箭头形状
请点此下载代码并用浏览器打开试玩. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-eq ...
- 【树形DP】 HDU 2412 Party at Hali-Bula
给出根节点(BOSS) 然后还有N-1个边 A B 由B指向A (B为A 的上司) 每次仅仅能选择这个关系中的当中一个 求最多选几个点 而且输出是不是唯一的 重点推断是否唯一: 1.若下属不去和去都 ...
- Python编程-数据库-利用PyMysql访问windows下的MySql数据库
1. 下载PyMysql并且安装 下载地址 下载zip包后解压到目录,进入该目录,执行以下命令安装 python setup.py install 2. 编写一个简单的数据库访问程序 simple_m ...
- javascript常用的方法(二)
//判断页面加载完毕 document.onreadystatechange = function () { if (document.readyState == "complete&quo ...
- Layer文件上传操作
1:upload.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- PYTHON如何降级?
到/usr/bin里面ls -l python*看看里面有多个版本的,把python2.6链接到python就可以了1.先把原来的删掉 rm python2.ln -s /usr/bin/python ...