【LeetCode】Missing Ranges
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的更多相关文章
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Wide and Deep Learning Model
https://blog.csdn.net/starzhou/article/details/78845931 The Wide and Deep Learning Model(译文+Tensorlf ...
- 学界 | Yann LeCun新作,中日韩文本分类到底要用哪种编码?
https://www.wxwenku.com/d/102093756 AI科技评论按:前几天,Yann LeCun与其学生 张翔在arXiv上发表了一篇新作「Which Encoding is th ...
- Apache Mahout:适合所有人的可扩展机器学习框架
http://www.ibm.com/developerworks/cn/java/j-mahout-scaling/ 在软件的世界中,两年就像是无比漫长的时光.在过去两年中,我们看到了社交媒体的风生 ...
- JS将"\/Date(1530104033000)\/" 格式化
JS将/Date(1446704778000)/转换成str: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) ...
- 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp
配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...
- GitHub万星项目:黑客成长技术清单
最近有个GitHub项目很火,叫"Awesome Hacking",这个项目是由Twitter账号@HackwithGithub 维护,喜欢逛Twitter的安全爱好者应该了解,在 ...
- ionic3+angular5页面间传递参数
一.从一个页面跳转到另一个页面的方法 1.引入服务 import { NavController } from 'ionic-angular'; 2.初始化 constructor(public na ...
- MVC 之 缓存机制(一)
一.概述 缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期.在系统优化过程中,缓存是比较 ...
- 【树莓派】使用xdrp远程登录树莓派的图形界面
之前采用了vnc方式方式的树莓派,但是配置还有点步骤,刚才看了一下,试验了一下xrdp,直接很简单就好了. 树莓派DIY笔记之前有介绍过用VNC连接到树莓派的方法.在Windows下,当然还是自带的远 ...
- Go语言中Socket通信TCP服务端
1.用法: (1)定义远程IP地址.使用net.ResolveTCPAddr()方法,定义一个TCP地址,做为本机监听地址. (2)使用net.ListenTCP("tcp",lo ...