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. Activity的几种启动跳转方式

     一.显示调用方法 •Intent intent=new Intent(this,OtherActivity.class); //方法1 •Intent intent2=new Intent(); • ...

  2. ArcMap - 分割.

    一,分割面: 1,在屏幕上新增线分割面: 使待编辑的面处于编辑状态 -> 选择待分割的面(使其处于选中状态) -> 选择编辑工具的 (Cut Polygons Tools) ->画线 ...

  3. 常用CDN公共库

    Jquery <script src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></scrip ...

  4. 一则自用iptables例子解释

    公网IP:110.24.3.83内网IP:10.252.214.186局域网数据库:10.252.214.100 通过NAT端口转发,访问110.24.3.83:3308端口跳转到局域网数据库机器的3 ...

  5. base64加密解密文件

    1 //字符串加密 -(void)demo1 { //普通的 8 bit二进制数据 NSString *str = @"hello world!"; //将字符串转换成二进制数据 ...

  6. Aspnet MVC 异步调用

    一个简图来描述下Aspnet MVC下的异步调用 { request } / \/ -------ISS------- > work thread | \ | \ route - aysn co ...

  7. mysql数据类型——TEXT和Blob

    TEXT是 以文本方式存储的,如果存储英文的话区分大小写  Blob是以二进制方式存储的,不区分大小写. xxxBlob存储的数据只能整体读出 有4种text类型:tinytext.text.medi ...

  8. Android 常用网站

    Android Design : http://www.sunjw.us/adchs/index.html Android Developers : http://developer.android. ...

  9. sys.argv[]用法

    #-*- coding: utf-8 -*- """ sys.argv 用来获取命令行参数 sys.argv[0] 表示当前执行文件 "-k".sta ...

  10. uboot总结:uboot配置和启动过程2(mkconfig分析)

    说明:文件位置:在uboot的目录下,文件名为:mkconfig.本身是一个脚本文件. 它的主要作用的是: (1)创建一个重要的符号链接 (2)创建一个config.mk文件(在include目录下) ...