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. 【BARTS计划】【Review_Week1】Google Docs 成为青少年们喜爱的聊天 app

    BARTS计划 · Review :每周阅读并点评至少一篇英文技术文章. 附原文链接 Google Docs 本是作为协作办公工具存在的,却成了学生们现代版“传纸条”的工具.认真的说,“你永远不知道用 ...

  2. Web项目Shiro总结及源码(十六)

    shiro过虑器 过滤器简称 对应的java类 anon org.apache.shiro.web.filter.authc.AnonymousFilter authc org.apache.shir ...

  3. 使用PowerMockito和Mockito进行模拟测试,包括静态方法测试,私有方法测试等,以及方法执行的坑或者模拟不成功解决

    依赖:这个很重要,不同版本用法也有点区别: <dependency> <groupId>org.mockito</groupId> <artifactId&g ...

  4. JDK8 Lambda表达式对代码的简化

    只是举个例子: public class LambdaDemo { public static String findData( String name , LambdaInterface finde ...

  5. High level GPU programming in C++

    https://github.com/prem30488/C2CUDATranslator http://www.training.prace-ri.eu/uploads/tx_pracetmo/GP ...

  6. ajax异步请求302

    我们知道,只有请求成功ajax才会进行回调处理,具体状态码为 status >= 200 && status < 300 || status === 304; 这一点通过查 ...

  7. linux更换yum源

    由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程: 1.删除redhat原有的yum rpm - ...

  8. sql 学习

    .查看表结构用desc desc emp;   2.空表dual,最常用的空表,如: select 2 * 4 from dual; select sysdate from dual;   3.双引号 ...

  9. aliyun服务器ecs被ddos后无法被zabbix-server监控的处理

    ecs绑定的域名被ddos攻击后,阿里云黑洞ecs服务器一个月,此时zabbix服务端无法联系到zabbix-agent会一直报错 解决办法: 1.在ecs前添加slb并把之前指向ecs的域名a.ch ...

  10. rem布局加载闪烁问题

    说明:以下内容来自CSDN,如有侵权,请立刻联系博主(我),我将删除该内容. 原文链接  https://blog.csdn.net/u013778905/article/details/779387 ...