[LeetCode#163] Missing Ranges
Problem:
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"].
General Analysis:
This kind problem is easy, it just test your proramming skills.
Basic idea:
According to the problem, the gap between two numbers: nums[i], nums[i+1] should be recorded.
There could be following situation:
------------------------------------------------------------------------------------------------
case 1: no gap (nums[i] + 1 == nums[i+1])
We need to record nothing.
------------------------------------------------------------------------------------------------
case 2: the gap's length is 1. (nums[i] + 2 = nums[i+1])
We need to record one number. nums[i]+1.
------------------------------------------------------------------------------------------------
case 3: the gap's length is larger than 1. (nums[i] + 2 < nums[i+1])
We need to record the range. [nums[i]+1, nums[i+1]-1]. Write in this way is a little ugly, what's more we have lower and upper must be included. we could use two variables for this purpose: (to compute the gap between nums[i-1] and nums[i])
------------------------------------------------------------------------------------------------
after: the number after nums[i-1]
pre: the number before nums[i]
------------------------------------------------------------------------------------------------
if (pre == after)
ret.add(pre + "");
else if (pre > after)
ret.add(after + "->" + pre);
after = nums[i] + 1; Skill:
To involve the lower and upper, we could assign "after" with "lower" before scanning the nums. And assign "pre" with "upper" after the scan. int after = lower;
int pre;
for (int i = 0; i < nums.length; i++) {
...
}
pre = upper;
if (pre == after)
ret.add(pre + "");
else if (pre > after)
ret.add(after + "->" + pre);
Wrong Solution:
public class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
if (nums == null)
throw new IllegalArgumentException("nums is null");
List<String> ret = new ArrayList<String> ();
if (nums.length == 0) {
String temp = lower + "->" + upper;
ret.add(temp);
return ret;
}
int after = lower;
int pre;
for (int i = 0; i < nums.length; i++) {
pre = nums[i] - 1;
if (pre == after)
ret.add(pre + "");
else if (pre > after)
ret.add(after + "->" + pre);
after = nums[i] + 1;
}
pre = upper;
if (pre == after)
ret.add(pre + "");
else if (pre > after)
ret.add(after + "->" + pre);
return ret;
}
}
Mistake Analysis:
Error case:
[], 1, 1
Output:
["1->1"]
Expected:
["1"] Mistake analysis:
I have failed to consdier the corner case when nums.length = 0, and lower and upper share the same value.
Thus we should not use "->".
Fix:
if (lower < upper) {
String temp = lower + "->" + upper;
ret.add(temp);
} else{
ret.add(lower + "");
}
Lesson: when the output should be genereated for different format (like "num1", "num1 -> num2"), you should be careful with you handling with corner case.
Solution:
public class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
if (nums == null)
throw new IllegalArgumentException("nums is null");
List<String> ret = new ArrayList<String> ();
if (nums.length == 0) {
if (lower < upper) {
String temp = lower + "->" + upper;
ret.add(temp);
} else{
ret.add(lower + "");
}
return ret;
}
int after = lower;
int pre;
for (int i = 0; i < nums.length; i++) {
pre = nums[i] - 1;
if (pre == after)
ret.add(pre + "");
else if (pre > after)
ret.add(after + "->" + pre);
after = nums[i] + 1;
}
pre = upper;
if (pre == after)
ret.add(pre + "");
else if (pre > after)
ret.add(after + "->" + pre);
return ret;
}
}
[LeetCode#163] Missing Ranges的更多相关文章
- 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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 163. Missing Ranges
题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...
- 【LeetCode】Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- LC 163. Missing Ranges 【lock, hard】
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode#159] Missing Ranges Strobogrammatic Number
Problem: Given a string, find the length of the longest substring T that contains at most 2 distinct ...
随机推荐
- Abstract Factory 抽象工厂模式
提供一个创建一些列相关或相互依赖对象的接口,而无需指定它们具体的类. 抽象工厂顾名思义就是对工厂的抽象,它提供了一组创建抽象产品对象的操作接口,我们实际使用的是抽象工厂的派生类,派生类中提供了操作的具 ...
- Android-关于屏幕适配的一些经验
刚开始,我开发时选取的模拟器是WVGA854,其分辨率为854*480.我开发完毕后装在800*480的手机上时感觉很OK,但是装到480*320.以及320*240分辨率上的手机时,很多界面都变形了 ...
- 浅谈href 和 src的区别
href 表示超文本引用(hypertext reference),在 link.a 等元素上.src 表示来源地址,在 img.script.iframe 等元素上.src 的内容,是页面必不可少的 ...
- dumpbin.exe
有时候我们想查看一个exe引用了哪些动态库,或者我们想看某个动态库包含哪些接口函数,这个时候可以使用dumpbin.exe工具: 1.输入Dumpbin -imports calldll.exe查看它 ...
- 《将博客搬至CSDN》的文章
我的CSDN地址 博客园应该以后会很少来了.
- Windows10 安装配置IIS,并将程序发布到服务器上
1.确保计算机链接网络(也可在不联网的时候使用安装包进行IIS的安装): 2.打开“控制面板”(“菜单”按钮+x 快捷键)——“程序”——“打开或关闭Windows功能”——展开“Internet信息 ...
- jsp中<c:if>与<s:if>的区别
<c:if>时jstl标签,一般与el表达式一起使用,参考http://www.360doc.com/content/11/1121/16/7874148_166229306.shtml ...
- OC - 13.数据解析(JSON与XML)
##数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需 ...
- html5 + css3 + zepto.js实现的微信广告宣传页
最新学习html5 + css3, 参考微信的一个推广页写出一个实例巩固自己知识,自己已经将原实例打包到自己博客文件当中,但是不知道如何提供下载,如有需要的朋友可以联系我qq309666726
- JS控制文字一个一个出现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...