Given a sorted integer array where the range of elements are in the inclusive range [lowerupper], 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"].

给出一个范围,和一个在这个范围内的数组,输出范围内缺失的位置。

提议很简单,就是需要注意的细节比较多:

1、len==0和len == 1的情况。

2、upper == Integer.MAX_VALUE的情况。

3、nums[i] > lower + 1 与 nums[i] - lower > 1相比,前者更好,因为后者有可能会溢出。

遍历这个数组,然后添加答案即可。

public class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List list = new ArrayList<String>();
int len = nums.length;
if (len == 0){
if (lower == upper){
list.add(String.valueOf(lower));
} else {
list.add(lower + "->" + upper);
}
return list;
}
for (int i = 0; i < len; i++){
if (nums[i] > lower + 1){
list.add(lower + "->" + (nums[i] - 1));
} else if (nums[i] - lower == 1){
list.add(String.valueOf(lower));
}
lower = nums[i]+1;
}
if (upper == lower){
list.add(String.valueOf(lower));
} else if ( nums[len-1] != Integer.MAX_VALUE && upper > lower){
list.add(lower + "->" + upper);
}
return list;
}
}

✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java的更多相关文章

  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

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

  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——Single Number(找出数组中只出现一次的数)

    问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  8. sql从某不连续的数字中将其分段并找出缺失的数字并分段

    首先做准备数据 )) ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') 将数据转换成应该处理的数据格式 ),colValue INT ) ) ,LEN(c ...

  9. 【LeetCode】Missing Ranges

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

随机推荐

  1. Rails,uva 514

    题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...

  2. wampserver You don't have permission to access / on this server. 解决 方法(转,正好碰到这样的事情了就转下来)

    最近在安装最近版wampserver 2.2 d时发现安装好后启动服务器,访问localhost显示You don't have permission to access / on this serv ...

  3. socket详解(一)《转》

    在客户/服务器通信模式中, 客户端需要主动创建与服务器连接的 Socket(套接字), 服务器端收到了客户端的连接请求, 也会创建与客户连接的 Socket. Socket可看做是通信连接两端的收发器 ...

  4. http://www.oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596529321

    集体智慧勘误表: http://www.oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596529321 ------------------- ...

  5. iOS开发 火星坐标转百度坐标

    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(latitude, longitude);//原始坐标 //转换 google地图.s ...

  6. mybaties 查询 语句正确 但是查不到结果

        控制台输出的语句拿到数据库查询就有数据,但是一直显示不出来  也不报错 后来  百度发现数据库连接不对  因为其中引用了两个数据库 所有写混了   ---------------------- ...

  7. 10月9日Android学习笔记:活动与服务之间的通信

    最近在照着<第一行代码>这本书来学安卓,顺便记下笔记.主要的内容是Android中服务的第二种启动方式,通过活动绑定服务来启动服务,实现活动与服务之间的通信. 一. 首先创建一个服务类 p ...

  8. android WebView总结

    http://blog.csdn.net/chenshijun0101/article/details/7045394 http://blog.csdn.net/ethan_xue/article/d ...

  9. c# 配置文件之configSections配置(二)

    在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System. ...

  10. JavaScript中的eval()函数

    和其他很多解释性语言一样,JavaScript同样可以解释运行由JavaScript源代码组成的字符串,并产生一个值.JavaScript通过全局函数eval()来完成这个工作. eval(“1+2” ...