[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 ...
随机推荐
- CentOS 6.7编译安装PHP7
1.首先配置好编译环境 yum update && yum upgrade yum groupinstall "Development Tools" yum ins ...
- 各种vpn协议介绍(重点介绍sslvpn的实现方式openvpn)
vpn介绍: VIrtual Private Network 虚拟专用网络哪些用户会用vpn? 公司的远程用户(出差.家里),公司的分支机构.idc机房.企业间.FQ常见vpn协议有哪些? ...
- HTML+CSS 整站 步骤
文件夹管理: CSS JS img font html 根据设计图,划分区块 ,即页面布局 重置样式 ;padding:0;} 写main.css 注意:1 距离尽量使用偶数,避免奇数 2 在使用定 ...
- final----这篇文章是我收获很大
final 用于声明属性.方法和类,分别表示属性不可变,方法不可重写,类不可继承. [转]Java final 修饰符知识点总结 final从字面上理解含义为“最后的,最终的”.在Java中也同样表示 ...
- phpMyAdmin教程 之 创建新用户/导入/导出数据库
盗用了被人的教程. 需要看就点击进去吧.复制过来实在是过意不去 http://www.wpdaxue.com/phpmyadmin-import-export-database.html
- Android - This Handler class should be static or leaks might occur.
今天学习了使用 HTTP协议,从Android客户端往Tomcat服务器端以GET发送请求,途中无意中发现自己写的Handler类被Android提示:This Handler class shoul ...
- Object-C 类实现
这篇为Object-C添加方法的后续. 这里我们应该在类的实现(.m)文件中写 #import "Photo.h" @implementation Photo - (NSStrin ...
- swift 截取字符串
- html5与js关于input[type='text']文本框value改变触发事件一些属性的区别oninput,onpropertychange,onchange和文本框的value点击全选状态onclick="select();"。做购物车页面时会要用到。
关于input[type='text']文本框value改变触发事件一些属性的区别oninput,onpropertychange,onchange和文本框的点击全选状态onclick="s ...
- 如何在本地安装测试ECSHOP 转载
如何在本地安装测试ECSHOP 如何在本地(自己的电脑)上先安装ECShop 一.创建PHP环境 1.下载AppServ 因为ECShop在线网上商店系统是用PHP语言开发的,所以,在本地架设网店之前 ...