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的更多相关文章
-
[LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
-
LeetCode Missing Ranges
原题链接在这里:https://leetcode.com/problems/missing-ranges/ 题目: Given a sorted integer array where the ran ...
-
✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
-
Missing Ranges &; Summary Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
-
163. Missing Ranges
题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...
-
[LeetCode#163] Missing Ranges
Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...
-
Missing Ranges 解答
Question Given a sorted integer array where the range of elements are in the inclusive range [lower, ...
-
LeetCode 163. Missing Ranges (缺失的区间)$
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
-
[Swift]LeetCode163. 缺失区间 $ Missing Ranges
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
随机推荐
-
实现python中的map函数
假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...
-
Linux安装FTP服务
转自:http://blog.csdn.net/huangbiao86/article/details/6641952 ftp服务器 1. 在Linux和其他机器之间共享文件(在linux下安装ft ...
-
C, C#, AS3的变量
高级语言中变量分值类型和引用类型, C中则分栈和堆, 在作为函数参数传递时,值类型复制,引用类型传递引用,不复制: 高级语言中一些基本类型默认是值类型, 其他基本都是引用类型. C语言中栈和堆变量可以 ...
-
POJ 2826 An Easy Problem?! --计算几何,叉积
题意: 在墙上钉两块木板,问能装多少水.即两条线段所夹的中间开口向上的面积(到短板的水平线截止) 解法: 如图: 先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上 ...
-
linux tomcat配置https
自己生成一个ssl证书,因为是自己生成的所以该证书,不被浏览器信任(具体表现https前面有个X) [root@centos apache-tomcat-]# keytool -genkey -ali ...
-
centos mysql 操作
安装mysqlyum -y install mysql-server 修改mysql配置 vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记 暂时修改一下编码(添加在密码下 ...
-
Java中关于String的split(String regex, int limit) 方法
今天在对一个String对象进行拆分的时候,总是无法到达预计的结果.呈现数据的时候出现异常,后来debug之后才发现,错误出在String spilt上,于是开始好好研究下这东西,开始对api里的sp ...
-
POJ	2246 Matrix Chain Multiplication
用栈来处理一下就好了. #include<iostream> #include<algorithm> #include<cstdio> #include<cs ...
-
BZOJ4556:[TJOI\HEOI2016]字符串(后缀数组,主席树,二分,ST表)
Description 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱 ...
-
C++ REST SDK i
Welcome! The C++ REST SDK is a Microsoft project for cloud-based client-server communication in nati ...