LeetCode Missing Ranges
原题链接在这里:https://leetcode.com/problems/missing-ranges/
题目:
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
Example:
Input: nums =[0, 1, 3, 50, 75]
, lower = 0 and upper = 99,
Output:["2", "4->49", "51->74", "76->99"]
题解:
If nums is null or empty, just add getRange(lower, upper).
Otherwise, first add range between lower and nums[0].
Then add missing range within nums.
Last, add range between nums[nums.length - 1] and upper.
Time Complexity: O(n). n = nums.length.
Space: O(1). regardless res.
AC Java:
class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> res = new ArrayList<>();
if(nums == null || nums.length == 0){
res.add(getRange(lower, upper));
return res;
} if(lower < nums[0]){
res.add(getRange(lower, nums[0] - 1));
} for(int i = 1; i<nums.length; i++){
if(nums[i - 1] != nums[i] && nums[i - 1] + 1 != nums[i]){
res.add(getRange(nums[i - 1] + 1, nums[i] - 1));
}
} if(nums[nums.length - 1] < upper){
res.add(getRange(nums[nums.length - 1] + 1, upper));
} return res;
} private String getRange(int l, int r){
if(l == r){
return "" + l;
} return l + "->" + r;
}
}
类似Summary Ranges, Data Stream as Disjoint Intervals.
LeetCode Missing Ranges的更多相关文章
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- LeetCode 163. Missing Ranges (缺失的区间)$
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [leetcode]163. Missing Ranges缺失范围
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [LeetCode#163] Missing Ranges
Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...
- 【LeetCode】Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- Missing Ranges -- LeetCode
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its mi ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
随机推荐
- BZOJ3726 : PA2014Final Wykladzina
从上到下枚举下底边,维护$a[i]$表示$i$向上延伸多少距离里面没有坏点,$b[i]$表示$i$向上延伸多少距离里面最多只有1个坏点. 设$l0[i],r0[i]$表示以$a[i]$为最小值,往左往 ...
- BZOJ1082: [SCOI2005]栅栏 题解
题目大意: 有一些木材,可以没有浪费地将一根木材分成几块木板(比如长度为10的木板可以切成长度为8和2的两块木板).现在你希望得到一些长度的木板,问通过分割木材最多能得到几块想要的木板. 思路: 首先 ...
- BNUOJ48605International Collegiate Routing Contest 题解
题目大意: 给你一些子网,求它们在整个网段的补集. 思路: 将子网转换成二进制建一棵Trie,直接DFS搜到没有了就记下来输出.注意:所给的子网会有交集,若搜到结尾就不向下搜了. 代码: #inclu ...
- 【HDU】4405 Aeroplane chess
http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:每次可以走1~6格,初始化在第0格,走到>=n的格子就结束.还有m个传送门,表示可以从X[i] ...
- BZOJ4546: codechef XRQRS
Description 给定一个初始时为空的整数序列(元素由1开始标号)以及一些询问: 类型1:在数组后面就加入数字x. 类型2:在区间L…R中找到y,最大化(x xor y). 类型3:删除数组最后 ...
- BestCoder Round #77
T1 xiaoxin juju needs help 计算组合数然后多重集排列乱搞,注意判无解情况(TM我就判错然后FST了). #include<cstdio> #include< ...
- Linux_awk命令详解
什么是awk? 你可能对UNIX比较熟悉,但你可能对awk很陌生,这一点也不奇怪,的确,与其优秀的功能相比,awk还远没达到它应有的知名度.awk是什 么?与其它大多数UNIX命令不同的是,从名字上看 ...
- asp.net 操作Excel大全
asp.net 操作Excel大全 转:http://www.cnblogs.com/zhangchenliang/archive/2011/07/21/2112430.html 我们在做excel资 ...
- 存储过程: 存储过程(stored procedure)有时也称为sproc。存储过程存储于数据库中而不是在单独的文件中,有输入参数、输出参数以及返回值等。
存储过程示例一: 执行存储过程方法一: 执行存储过程方法二: 存储过程可以定义返回值: 修改存储过程: 利用存储过程查找三个表内的信息: 练习: 超市管理系统:表一:门店仓库表 MenDian ...
- javax.servlet.jsp cannot be resolved to a type
参考链接 :http://www.tuicool.com/articles/7Njmqy