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"].

分析:

  直接遍历即可,特别要注意的是,边界情况需要想清楚

代码:

vector<string> missingRanges(vector<int> &num, int lower, int upper) {
vector<string> mr;
//1、为空时直接返回范围
if(num.empty()) {
mr.push_back(lower == upper ? to_string(lower) : to_string(lower) + "->" + to_string(upper));
return mr;
}
//2、前面留有空隙
if(num.front() > lower)
mr.push_back(num.front() - == lower ? to_string(lower) : to_string(lower) + "->" + to_string(num.front() - ));
//3、中间留有空隙
for(int i = ; i < num.size(); i++) {
if(num[i] == num[i - ] + )
continue;
int first = num[i - ] + , last = num[i] - ;
mr.push_back(first == last ? to_string(first) : to_string(first) + "->" + to_string(last));
}
//4、最后留有空隙
if(num.back() < upper)
mr.push_back(num.back() + == upper ? to_string(upper) : to_string(num.back() + ) + "->" + to_string(upper));
return mr;
}

[Locked] Missing Ranges的更多相关文章

  1. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

  2. LeetCode Missing Ranges

    原题链接在这里:https://leetcode.com/problems/missing-ranges/ 题目: Given a sorted integer array where the ran ...

  3. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  4. Missing Ranges & Summary Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  5. 163. Missing Ranges

    题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...

  6. [LeetCode#163] Missing Ranges

    Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...

  7. Missing Ranges 解答

    Question Given a sorted integer array where the range of elements are in the inclusive range [lower, ...

  8. LeetCode 163. Missing Ranges (缺失的区间)$

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  9. [Swift]LeetCode163. 缺失区间 $ Missing Ranges

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

随机推荐

  1. HBase0.98.1 通过协调器进行表的行数统计

    1. 启用表aggregation,只对特定的表生效.通过HBase Shell 来实现 (1)disable指定表.hbase> disable 'student' (2)添加aggregat ...

  2. JAVA package与import机制

    JAVA package与import机制 http://files.cnblogs.com/files/misybing/JAVA-package-and-import.pdf import org ...

  3. 多列的行列转换(PIVOT,UNPIVOT)

    形式1 形式2 形式3 有时候可能会有这样的需求: 将一张表的所有列名转做为数据的一列数据,将一列数据作为整张表的列名 当列比较多时,只用PIVOT是解决不了的,经过研究,需要将UNPIVOT 和 P ...

  4. 自己动手写一个iOS 网络请求库的三部曲[转]

    代码示例:https://github.com/johnlui/Swift-On-iOS/blob/master/BuildYourHTTPRequestLibrary 开源项目:Pitaya,适合大 ...

  5. 【转】UILabel、UITextView自适应得到高度

    原文:http://blog.csdn.net/xcysuccess3/article/details/8331549 在iOS中,经常遇到需要根据字符串的内容动态指定UILabel,UITextVi ...

  6. UITableViewCell 左滑删除

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...

  7. centos 6.x 安装redis

    1.yum 安装 yum install redis 如果提示找不到包的话  可以yum install epel-release   先安装epel第三方库 2.源码安装 https://redis ...

  8. Poj 3667

    这是第一题线段树的区间合并的题: 这类的题用于求连续的最长长度什么的: 这题我看的是一篇比较不错的博客: 我把我的理解注释在代码里了: #include <iostream>#includ ...

  9. SQL Server Analysis Services 数据挖掘(1)

    来源: http://technet.microsoft.com/zh-cn/library/dn633476.aspx 假如你有一个购物类的网站,那么你如何给你的客户来推荐产品呢?这个功能在很多 电 ...

  10. JSP HTML区别

    1.最简单的区别就是,HTML能直接打开,jsp只能发布到Tomact等服务器上才能打开2.定义上HTML页面是静态页面可以直接运行,JSP页面是动态页它运行时需要转换成servlet.3.他们的表头 ...