Difficulty: Medium

 More:【目录】LeetCode Java实现

Description

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its
missing ranges.
For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”]
Example Questions Candidate Might Ask:
Q: What if the given array is empty?
A: Then you should return [“0->99”] as those ranges are missing.
Q: What if the given array contains all elements from the ranges?
A: Return an empty list, which means no range is missing.

Intuition

本题数字范围从[0, 99]拓展为[start,end]

方法一(自己开始时想的):遍历数组,确定每个缺失区域的开头和结尾。这里需要对数组的第一位和最后一位是否等于start和end单独讨论。

方法二(简单点):遍历数组,使用cur和pre指针指向当前数字和前一个数字,通过pre和cur的差是否为1可以得到缺失区域的开头和结尾。开始时,pre设置为start-1;结束时,cur设置为end+1,这样可以避免对数组首尾单独进行讨论。

注意:1.数组为空时,应该返回[start->end];

   2.如何返回缺失的数字范围?采用List<String>返回。

Solution

    //方法一
public List<String> getMissingRanges(int[] arr,int start,int end){
List<String> list = new ArrayList<String>();
if(arr==null || arr.length<=0) {
list.add(getRange(start, end));
return list;
}
if(arr[0]!=start)
list.add(getRange(start,arr[0]-1));
for(int k=1;k<arr.length;k++) {
if( arr[k]==arr[k-1]+1)
continue;
list.add(getRange(arr[k-1]+1, arr[k]-1));
}
if(arr[arr.length-1]!=end)
list.add(getRange(arr[arr.length-1]+1,end));
return list;
} //方法二
public List<String> getMissingRanges2(int[] arr,int start,int end){
List<String> list = new ArrayList<String>();
int pre=start-1;
for(int i=0;i<=arr.length;i++) {
int cur = (i==arr.length) ? end+1 : arr[i];
if(cur-pre>=2)
list.add(getRange(pre+1, cur-1));
pre=cur;
}
return list;
} private String getRange(int from, int to) {
return (from==to)? ""+from : from+"->"+to;
}

  

Complexity

Time complexity : O(n)

Space complexity :  O(1)

What I've learned

1.数组为空时,应该返回[start->end]而不是返回null。

2.使用List<String>数据结构来返回返回缺失的数字范围。

3.方法二中pre和cur就是已知的数字,可以推得方法一中所要的缺失首尾,所以更加方便。

 More:【目录】LeetCode Java实现

【LeetCode】163. Missing Range的更多相关文章

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

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

  2. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  3. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

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

  4. 【LeetCode】268. Missing Number 解题报告(Java & Python)

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

  5. 【LeetCode】632. Smallest Range 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...

  6. 【leetcode】First Missing Positive

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  7. 【leetcode】First Missing Positive(hard) ☆

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  8. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  9. 【leetcode】1228.Missing Number In Arithmetic Progression

    题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...

随机推荐

  1. C# Regex正则常用方法的使用

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  2. 关于Laravel 无法下载的问题

    今天在git bush用composer安装laravel5.5卡住了,无法下载 解决办法:更换到国内源,就可以下载了 运行命令: composer config -g repo.packagist ...

  3. Responsive响应式设计

    在IE6-8中完全是不支持CSS3 Media Queries的.那么为了让IE6-8支持,我们就很有必要的在IE9以下的浏览器中加上media-queries.js或者respond.js脚本: & ...

  4. C# List分页

    假设你每页10条数据当前是第3页 跳到第4页则:List.Skip((4-1)*10).Take(10) 本文来自SunShine,转载请标明出处: http://do.jhost.cn/sunshi ...

  5. shiro自定义realm认证(五)

    上一节介绍了realm的作用: realm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证信息,如果查询不到返回null.token就相当于是对用户输入 ...

  6. linux查看操作系统的版本

    内核信息 uname -a localhost.localdomain:所在主机的主机名,与主机配置文件/etc/hosts内容一致 2.4.20-8#1:内核版本号 Thu Mar 13 17:18 ...

  7. 【API】恶意样本分析手册——API函数篇

    学编程又有材料了 http://blog.nsfocus.net/malware-sample-analysis-api/

  8. Pytorch之可视化

    先解决下keras可视化安装graphviz的问题: 注意安装顺序: sudo pip3 install graphviz # python包 sudo apt-get install graphvi ...

  9. Raw Socket vs Stream Socket vs datagram socket,原始套接字与流式套接字与数据报套接字

    https://opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/ In this tutorial, let’s take a look ...

  10. PYTHON-UDP

    1.TCP 和 UDP 发送数据时的流程 ***** 解释 为何TCP是可靠的 是因为发送数据后必须收到确认包 2. UDP的模板代码 ***** 1.UDP协议: (数据报协议) 特点: 无连接 优 ...