[Locked] Missing Ranges

时间:2021-07-22 08:43:43

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. &lbrack;LeetCode&rsqb; 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&period; Missing Ranges 找出缺失范围 --------- java

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

  4. Missing Ranges &amp&semi; Summary Ranges

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

  5. 163&period; Missing Ranges

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

  6. &lbrack;LeetCode&num;163&rsqb; 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&period; Missing Ranges (缺失的区间)&dollar;

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

  9. &lbrack;Swift&rsqb;LeetCode163&period; 缺失区间 &dollar; Missing Ranges

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

随机推荐

  1. 实现python中的map函数

    假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...

  2. Linux安装FTP服务

    转自:http://blog.csdn.net/huangbiao86/article/details/6641952 ftp服务器 1.  在Linux和其他机器之间共享文件(在linux下安装ft ...

  3. C&comma; C&num;&comma; AS3的变量

    高级语言中变量分值类型和引用类型, C中则分栈和堆, 在作为函数参数传递时,值类型复制,引用类型传递引用,不复制: 高级语言中一些基本类型默认是值类型, 其他基本都是引用类型. C语言中栈和堆变量可以 ...

  4. POJ 2826 An Easy Problem&quest;&excl; --计算几何,叉积

    题意: 在墙上钉两块木板,问能装多少水.即两条线段所夹的中间开口向上的面积(到短板的水平线截止) 解法: 如图: 先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上 ...

  5. linux tomcat配置https

    自己生成一个ssl证书,因为是自己生成的所以该证书,不被浏览器信任(具体表现https前面有个X) [root@centos apache-tomcat-]# keytool -genkey -ali ...

  6. centos mysql 操作

    安装mysqlyum -y install mysql-server 修改mysql配置 vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记 暂时修改一下编码(添加在密码下 ...

  7. Java中关于String的split&lpar;String regex&comma; int limit&rpar; 方法

    今天在对一个String对象进行拆分的时候,总是无法到达预计的结果.呈现数据的时候出现异常,后来debug之后才发现,错误出在String spilt上,于是开始好好研究下这东西,开始对api里的sp ...

  8. POJ&Tab;2246 Matrix Chain Multiplication

    用栈来处理一下就好了. #include<iostream> #include<algorithm> #include<cstdio> #include<cs ...

  9. BZOJ4556&colon;&lbrack;TJOI&bsol;HEOI2016&rsqb;字符串&lpar;后缀数组&comma;主席树&comma;二分&comma;ST表&rpar;

    Description 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱 ...

  10. C&plus;&plus; REST SDK i

    Welcome! The C++ REST SDK is a Microsoft project for cloud-based client-server communication in nati ...