Missing Ranges 解答
Question
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], 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 解答的更多相关文章
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- LeetCode Missing Ranges
原题链接在这里:https://leetcode.com/problems/missing-ranges/ 题目: Given a sorted integer array where the ran ...
- ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- Missing Ranges & Summary Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- 163. Missing Ranges
题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...
- [LeetCode#163] Missing Ranges
Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...
- [Locked] Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- LeetCode 163. Missing Ranges (缺失的区间)$
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [Swift]LeetCode163. 缺失区间 $ Missing Ranges
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
随机推荐
- android动画效果编程基础--Android Animation
动画效果编程基础--Android Animation 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 tran ...
- MultiWii MWC的软件和调试方法
(如果你的电脑是win7 64位的系统,安装了JAVA虚拟机后GUI仍然运行不了,那你就需要到C:\Program Files\Java\jre7\bin\找到并复制javaw.exe,然后粘贴到C: ...
- EventBus 事件总线 案例
简介 地址:https://github.com/greenrobot/EventBus EventBus是一个[发布 / 订阅]的事件总线.简单点说,就是两人[约定]好怎么通信,一人发布消息,另外一 ...
- jdbc01
1.创建对应的数据库以及表 /* SQLyog 企业版 - MySQL GUI v8.14 MySQL - 5.5.32-log : Database - news ***************** ...
- SIEBEL安装问题
安装siebel 分三步走: 1.安装oracle 11g 2.安装Client 3.分别安装siebel tools.siebel web client,之后打上补丁 安装siebel tools. ...
- OD: SEHOP
SEHOP,Structed Exception Handling Overwrite Protection,一种比 SafeSEH 更严厉的保护机制.Windows Vista SP1 开始支持 S ...
- CentOS目录结构详解
CentOS是文件管理系统,在CentOS中所有的程序都以文件形式存储.初学CentOS的朋友需要了解各个挂载点 目录的结构和作用.这样才能更好的去管理系统. CentOS的目录大体上可分为四类:管理 ...
- JAVA-5-关于for循环的几个例子
打印一个*组成的矩形 public static void main(String[] args) { // TODO 自动生成的方法存根 for (int i = 0; i < 5; i++) ...
- The partial sum problem
算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...
- nuc950支持nand的mtd驱动的kernel修改
支持nand的mtd驱动的kernel修改 一.更新nanddriver文件 将新的nanddriver文件nuc900_nand.c放到kernel的drivers/mtd/nand目录下 二.修改 ...