【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 ...
随机推荐
- vue组件的hover事件模拟、给第三方组件绑定事件不生效问题
1.vue里面实现hover效果基本需要用事件模拟 <div @mouseover="overShow" @mouseout="outHide"> ...
- java 获取指定日前的前一天
/** * @Author: * @Description:更加输入日期,获取输入日期的前一天 * @Date: * @strData:参数格式:yyyy-MM-dd * @return:返回格式:y ...
- Android -- Drag&&Drop
Android3.0提供了drag/drop框架,利用此框架可以实现使用拖放手势将一个view拖放到当前布局中的另外一个view中. 实现拖放的步骤 首先,我们先了解一下拖放过程,从官方文档可以知道, ...
- 【Java】Java-fastjson-基本使用方法
Java-fastjson-基本使用方法 fastjson maven_百度搜索 Maven Repository: com.alibaba » fastjson » 1.2.44 fastjson ...
- 双数组Trie的一种实现
An Implementation of Double-Array Trie 双数组Trie的一种实现 原文:http://linux.thai.net/~thep/datrie/datrie.htm ...
- hadoop 2.2搭建常见错误
http://blog.csdn.net/haidao2009/article/details/14897813 hadoop 2.2 搭建 http://blog.csdn.net/pelick/a ...
- tail -f 然后grep,处理缓存的问题
学习了:http://www.quwenqing.com/read-134.html 对日志记录做多次grep过滤输出,格式如下: tail -f log | grep xxx | grep yyy ...
- mongodb自动关闭:页面文件太小,无法完成操作
在一台两G内存的win server 2008电脑上运行一个程序,一段时间后mongod自动停止,发现日志文件最后有这样的错误: 2014-11-30T00:32:32.914+0800 [conn3 ...
- Python代码规范(PEP8)问题及解决
转载:https://blog.csdn.net/Jason_Lewis/article/details/75386598 最近刚刚接触Python,为了养成好习惯,尽量保证自己写的代码符合PEP8代 ...
- 多模块Maven项目如何使用javadoc插件生成文档
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 需求 最近要对一个项目结构如下的Maven项目生成JavaDoc文档. Project ...