[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 ...
随机推荐
- Tcp抓包以及tcp状态解释
tcp三次握手 发送端发送一个SYN=1,ACK=0标志的数据包给接收端,请求进行连接,这是第一次握手:接收端收到请求并且允许连接的话,就会发送一个SYN=1,ACK=1标志的数据包给发送端,告诉它, ...
- Linq扩展方法之Aggregate 对序列应用累加器函数
Linq扩展方法之Aggregate 对序列应用累加器函数; 函数模板:// 函数名:对序列应用累加器函数. // Parameters:参数要求 // source:要聚合的 System.Col ...
- [转]JavaScript 的同源策略
同源策略限制了一个源(origin)中加载文本或脚本与来自其它源(origin)中资源的交互方式. 同源定义 如果两个页面拥有相同的协议(protocol),端口(如果指定),和主机,那么这两个页面就 ...
- [日历] C#修改CNDate日历帮助类 (转载)
点击下载 CNDate.rar 主要功能如下 .传回公历y年m月的总天数 .根据日期值获得周一的日期 .获取农历 #region 私有方法 private static long[] lunarInf ...
- “jni.h”: No such file or directory
VS2010解决方案: 进入 “包含目录“ 方式: 右键项目属性页-> 配置属性->VC++目录->包含目录 在”包含目录“中编辑 添加以下路径: C:\Program Files\ ...
- 3 委托、匿名函数、lambda表达式
委托.匿名函数.lambda表达式 在 2.0 之前的 C# 版本中,声明委托的唯一方法是使用命名方法.C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方 ...
- 校省选赛第一场C题解Practice
比赛时间只有两个小时,我没有选做这题,因为当时看样例也看不懂,比较烦恼. 后来发现,该题对输入输出要求很低.远远没有昨天我在做的A题的麻烦,赛后认真看了一下就明白了,写了一下,一次就AC了,没问题,真 ...
- MVVM模式应用 之为ApplicationBarIconButton 添加Command操作属性
在学习MVVM的过程中,总是会遇到挫折,一碰到就是花费好长时间去解决..唉,不求量,只求质. 第一种(已经实践成功): (1)http://bindableapplicationb.codeplex. ...
- OSPF + LVS ,突破LVS瓶颈 (转)
突破LVS瓶颈,LVS Cluster部署(OSPF + LVS) 前言 架构简图 架构优势 部署方法 1.硬件资源准备 2.三层设备OSPF配置 3.LVS调度机的OSPF配置 a.安装软路由软件q ...
- jQuery动态添加的元素中处理字符串溢出后在指定字符数后添加省略号
"+[jsonData[i].questitle.lenth>40?jsonData[i].questitle.substring(0,40)+"...":json ...