Question

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"].

Answer

遍历数组,如果arr[i] > arr[i-1] + 1则说明arr[i]与arr[i-1]中有missing range。注意考虑开头和结尾。

 public class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
addRange(result, lower, upper);
return result;
}
int len = nums.length;
// process head
if (nums[0] > lower) {
addRange(result, lower, nums[0] - 1);
}
for (int i = 1; i < len; i++) {
if (nums[i] > nums[i - 1] + 1) {
addRange(result, nums[i - 1] + 1, nums[i] - 1);
}
}
// process tail
if (nums[len - 1] < upper) {
addRange(result, nums[len - 1] + 1, upper);
}
return result;
} private void addRange(List<String> result, int low, int high) {
StringBuilder sb = new StringBuilder();
sb.append(low);
if (high > low) {
sb.append("->");
sb.append(high);
}
result.add(sb.toString());
}
}

Missing Ranges 解答的更多相关文章

  1. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

  2. LeetCode Missing Ranges

    原题链接在这里:https://leetcode.com/problems/missing-ranges/ 题目: Given a sorted integer array where the ran ...

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

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

  4. Missing Ranges & Summary Ranges

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

  5. 163. Missing Ranges

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

  6. [LeetCode#163] Missing Ranges

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

  7. [Locked] Missing Ranges

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

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

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

  9. [Swift]LeetCode163. 缺失区间 $ Missing Ranges

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

随机推荐

  1. [CSS3] CSS Media Queries

    Using CSS media queries allows you to design responsive layout in your web apps and website. We will ...

  2. android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)

    效果图 :                         分别是第一页.第二页.第三页,随手截的图,不整齐,勿见怪.开始走了弯路,废了不少时间. 思路如下: 1.用ViewPager实现左右分页滑动 ...

  3. Target runtime Apache Tomcat v6.0 is not defined. phyy Unknown Faceted Project Problem

    Description Resource Path Location TypeTarget runtime Apache Tomcat v6.0 is not defined. phyy Unknow ...

  4. Chapter 8. Introduction to multi-project builds 多工程构建介绍

    Only the smallest of projects has a single build file and source tree, unless it happens to be a mas ...

  5. linux定时任务2-at命令

    定时执行命令: [root@rheltest1 ~]# at now + 1 minute //一分钟后执行下面的任务 at> date >> /tmp/test_date.log ...

  6. nginx 站点80跳443配置

    server { listen 80; server_name www.furhacker.cn; location /{# return 301; rewrite ^(.*)$ https://$h ...

  7. 使用Application_Error捕获站点错误并写日志

    Global.ascx页面使用以下方法即可捕获应用层没有try cath的错误 protected void Application_Error(Object sender, EventArgs e) ...

  8. Eclipse自动提示功能

    一般默认情况下,Eclipse的代码提示功能是比MicrosoftVisualStudio的差很多的,主要是Eclipse本身有很多选项是默认关闭的,要开发者自己去手动配置.如果开发者不清楚的话,就不 ...

  9. 关于char与varchar,varchar2的区别

    http://zhidao.baidu.com/question/220360696.html?qbl=relate_question_0&word=char%BA%CDvarchar2%B5 ...

  10. WPFPath素材

    放在Viewbox中可固定比例 <!--五角星--> <Canvas Width="100" Height="100"> <Pat ...