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. UITableView显示不全

    先上图(不全图片): 正确图片: 原因例如以下: 1.在tableView的父视图的freme问题. 2.tableView本身的frame问题.大小依据自己的实际情况改过来就OK了 希望能够帮助到你 ...

  2. TinyXml快速入门(二)

    在<TinyXml快速入门(一)>中我介绍了使用TinyXml库如何创建和打印xml文件,下面我介绍使用tinyxml库对xml文件进行一系列的操作,包括获取xml文件声明,查询指定节点. ...

  3. hdu1074 Doing Homework(状态压缩DP Y=Y)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. View原理

    View处理: 绘制(paint canvas path:tween等动画效果).事件处理   参考整理自: Custom Components: http://developer.android.c ...

  5. JAVA虚拟机与内存

    资料整理自网络(侵删) JVM内存 组成 JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 栈区: 1.每个线程包含一个栈区,栈中只保存基础数据类型的对象和 ...

  6. Hibernate 关联查询 相关错误

    错误提示: could not resolve property: 确定有相关属性时,记得使用 criteria.createAlias @ManyToOne 若可能为null 要加上 @NotFou ...

  7. maven 配置报错 JAVA_HOME not found in your environment

    最近比较空,想研究下spring mvc,于是编按照教程一步一步配置开发环境.配置maven完成后,运行命令mvn -v的时候,竟然报错.错误信息如下: Error: JAVA_HOME not fo ...

  8. Lesson 6: Exploring the World of Typefaces

    Lesson 6: Exploring the World of Typefaces 这课提到的字体都是 英文 的. Article 1: More Google Web Fonts That Don ...

  9. dp、sp 转换为 px 的工具类

    public class DisplayUtil { /** * 将px值转换为dip或dp值,保证尺寸大小不变 * * @param pxValue (DisplayMetrics类中属性densi ...

  10. Cer Crt Pem Pfx 证书格式转换

    1.从pfx格式的证书提取出密钥和证书set OPENSSL_CONF=openssl.cnfopenssl pkcs12 -in my.pfx -nodes -out server.pemopens ...