Problem:

Given a sorted integer array where the range of elements are [lowerupper] 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的更多相关文章

  1. LeetCode 163. Missing Ranges (缺失的区间)$

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  2. [leetcode]163. Missing Ranges缺失范围

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  3. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  4. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  5. 【LeetCode】163. Missing Ranges 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  6. 163. Missing Ranges

    题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...

  7. 【LeetCode】Missing Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  8. LC 163. Missing Ranges 【lock, hard】

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  9. [LeetCode#159] Missing Ranges Strobogrammatic Number

    Problem: Given a string, find the length of the longest substring T that contains at most 2 distinct ...

随机推荐

  1. Bash判断文件是否存在

    #!/bin/bash if [ -f filename ]; then echo 'file exist' else echo 'file not exist' fi

  2. SCIP读书笔记(1)

    这书也算是必修吧,尤其是我这种非科班人员,还是应该抽时间尽量学习一下.大致翻过一遍,习题非常多,尽力吧. ##构造过程抽象 * 为了表述认知,每种语言都提供了三种机制:基本元素:组合方式:抽象方法. ...

  3. JAVA SE 框架之俄罗斯方块的效果

    Mygame package com.sun.c; import java.awt.event.KeyListener; import com.sun.v.MyJpanel; import com.s ...

  4. CSS制作hover下划线动画

    .demo1{ position: relative; text-decoration: none; font-size: 20px; color: #333; } .demo1:before{ co ...

  5. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  6. Object之魔术函数__call() 处理错误调用

    在提到__call之前,先来看一个实例的测试结果,以便更好地去了解__call方法的作用.上代码: <?php class Person{ function say(){ echo " ...

  7. Python 手册——开胃菜

    如果你写过大规模的Shell脚本,应该会有过这样的体会:你还非常想再加一些别的功能进去,但它已经太大. 太慢.太复杂了:或者这个功能需要调用一个系统函数,或者它只适合通过C来调用……通常这些问题还不足 ...

  8. 工欲善其事必先利其器-Notepad++使用小记(Python)

    大学开始就一直使用Notepad++ 作为代码编辑器,喜欢它的简洁明了,喜欢它的个性,也喜欢它各种各样骚气的插件. 今天闲来无事,写篇文章记录一下平时使用的种种,包括但不限于个性化使用一些宏,快捷键, ...

  9. PyQt5创建第一个窗体(正规套路)

    一.Pyqt5 创建第一个窗体 很多人写窗体程序都是直接敲代码,不使用设计器,我个人不是很赞成这种做法.使用设计器的好处是直观.维护方便,尤其开发复杂窗体的效率高. 但是每次修改ui文件后,需要重新生 ...

  10. 编程思想—依赖注入(DI)并非实现控制反转(IOC)的最佳方法

    以构造函数注入为例: public class TestClass(IClassA a,IClassB b, IClassC C,IClassD d) { public void Method1() ...