【LeetCode】163. Missing Range
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的更多相关文章
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 【leetcode】1228.Missing Number In Arithmetic Progression
题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...
随机推荐
- C++中返回值
函数的返回值用于初始化在调用函数是创建的临时对象. 1.返回值为非引用类型: 会将函数的返回值复制给临时对象.跟实参初始化形参的方式一样. 2.返回值为引用类型: 没有复制返回值,返回的是对象本身.返 ...
- Python之Eclipse环境下安装与配置
奔着对python的好奇,今天又是周末,欲小试Python.那么首先避不开的问题就是python的环境搭建.而我之前已经在学习Java的过程中安装了Eclipse,不想再安装更多的IDE了,就那Ecl ...
- python中的%s%是什么意思
它是一个字符串格式化语法(它从C借用). 请参阅 “格式化字符串”: Python支持将值格式化为字符串.虽然这可以包括非常复杂的表达式,但最基本的用法是将值插入到%s 占位符的字符串中 . 编辑 ...
- 404错误处理以及以后缀为action结尾的处理
--------------第一种是胡乱敲,后缀不是以action结尾,出现404错误-----------------------------
- DockerFile解析
⒈是什么? DockerFile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本文件. ⒉步骤 ①手动编写一个符合规范的DockerFile文件(编写) ②使用docker bui ...
- 解决mysql开启GTID主从同步出现1236错误问题【转】
最近遇到mysql开启gtid做复制时,从库出现1236错误,导致同步无法进行,本文就这问题记录下处理步骤,有关gtid知识在这里不做介绍,mysql版本为5.7.16. 一.错误原因分析 错误信息如 ...
- C# 清理消息管道的消息
using System;using System.Collections.Generic;using System.Linq;using System.Messaging;using System. ...
- windows环境用python修改环境变量的注意点(含代码)
1.部分环境变量字段需要保留原来的值,只是做添加,不可以替换 2.Path和PATH对于python来说是一样的,也就是说存在名为Path的环境变量时,添加PATH的环境变量,会覆盖原有的Path环境 ...
- NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{HBmUtjMOQP2pgLFFwqa_Og}{172.16.0.163}{172.16.0.163:9300}] ]
1.找到elasticsearch的安装目录,在config目录找到elasticsearch.yml,查看cluster.name的赋值 2.在SpringBoot的yml文件中,不仅要配置clus ...
- centos6.8系统安装 Hadoop 2.7.3伪分布式集群
安装 Hadoop 2.7.3 配置ssh免密码登陆 cd ~/.ssh # 若没有该目录,请先执行一次ssh localhost ssh-keygen - ...