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的更多相关文章

  1. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  2. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  3. 【leetcode】41. First Missing Positive

    题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...

  4. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  5. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Anciroid的IPC机制-Binder原理

    Binder驱动的原理和实现 通过上一节的介绍,大家应该对Binder有了基本的认识了.任何上层应用程序接口和用户操作都需要底层硬件设备驱动的支持,并为其提供各种操作接口.本节首先从Binder的驱动 ...

  2. Python机器学习实践与Kaggle实战(转)

    https://mlnote.wordpress.com/2015/12/16/python%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%AE%9E%E8%B7%B5 ...

  3. ThinkPHP3.2多域名 Virtual .htaccess 匹配 RewriteCond %{HTTP_HOST}

    <Files *> Options -Indexes </Files> <IfModule mod_headers.c> <FilesMatch " ...

  4. linux下安装与删除软件

    linux下安装与删除软件 (2005-07-04 11:24:10) 转载▼ 标签: 杂谈 分类: MSN搬家 现在linuxx下的软件大都是rpm,deb.tar.gz和tar.bz2格式.1.r ...

  5. UML关系(泛化,实现,依赖,关联(聚合,组合))

    http://www.cnblogs.com/olvo/archive/2012/05/03/2481014.html UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 继承.实现.依赖. ...

  6. WIN10系统 如何自动调整系统时间

    在底部搜索框中输入设置   先关闭再打开自动设置时间的选项即可重新获取系统时间,前提要联网状态

  7. kendoUpload

    <style> .upfile { display: inline-block; width: %; } .upfile li { display: inline-block; width ...

  8. Jaspersoft Studio 导出PDF格式中文不显示

    1:设置字体 2:应用上面设置的字体

  9. FutureTask使用完整演示样例

    MainActivity例如以下: package cc.cv; import java.util.concurrent.FutureTask; import android.os.Bundle; i ...

  10. 理解CPU steal time

    http://melody-dc.com/2015/11/21/%E7%90%86%E8%A7%A3CPU-steal-time/ http://www.cnblogs.com/yjf512/p/33 ...