题目地址:https://leetcode-cn.com/problems/missing-ranges/

题目描述

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.

Example:

Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
Output: ["2", "4->49", "51->74", "76->99"]

题目大意

给定一个排序的整数数组 nums ,其中元素的范围在 闭区间 [lower, upper] 当中,返回不包含在数组中的缺失区间。

解题方法

遍历

这个题的坑比较多,比如:

  1. 小心整数操作溢出
  2. 区间中数据可能有重复
  3. 处理第一个和最后一个节点

采用long long类型的数据,然后依次遍历所有的元素,判断这个元素和上一个元素的差值是否>=2。另外需要注意最后一个元素也要判断。

第一次提交的时候,我想在nums.push_back(upper+1),只要一次遍历就行。结果发现哦upper+1可能会超出int范围。所以只能用下面的解法了。

C++代码如下:

class Solution {
public:
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
vector<string> res;
long long left = (long long)lower - 1;
long long val = 0;
for (long long right : nums) {
val = right - left;
if (val == 2) {
res.push_back(to_string(left + 1));
} else if (val > 2) {
res.push_back(to_string(left + 1) + "->" + to_string(right - 1));
}
left = right;
}
val = upper - left;
if (val == 1) {
res.push_back(to_string(upper));
} else if (val > 1) {
res.push_back(to_string(left + 1) + "->" + to_string(upper));
}
return res;
}
};

参考资料:https://leetcode-cn.com/problems/missing-ranges/solution/cte-shu-qing-kuang-duo-dao-bao-by-liyupi/

日期

2019 年 9 月 23 日 —— 昨夜睡的早,错过了北京的烟火

【LeetCode】163. Missing Ranges 解题报告 (C++)的更多相关文章

  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 找出缺失范围 --------- java

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

  5. [LeetCode#163] Missing Ranges

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

  6. LeetCode: First Missing Positive 解题报告

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  7. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. Debugging and Running MPI in Xcode

    1.安装 mpich2 与 Xcode mpich2安装地址:/usr/local/Cellar/mpich2/3.1.4/ Xcode 版本:Version 6.2 (6C131e) 2.新建工程 ...

  2. 如何根据taxid(或taxname)快速获得taxname(或taxid)?

    目录 需求 实现 需求 我有一个物种taxonomy ID的list,想获得相应的物种名,不要一个个去NCBI Taxonomy官网查.反之根据物种名list查询对应的taxid. 实现 因为之前没怎 ...

  3. Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache RocketMQ官网地址 https://rocketmq.apache.org/ Latest rel ...

  4. 零基础学习java------day10------带包编译,权限修饰符,内部类,调式,junitTest

    0.  带包编译 解决使用notepad++编写的java类中如果有package的解决方案,如下代码 package com._51doit.test; class HelloWorld{ publ ...

  5. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

  6. java 整型

    byte(1字节).short(2字节).int(4字节).long(16字节) java中前缀加上0b或者0B就可以写二进制数,前缀加上0就可以写八进制数,前缀加上0x或者0X就可以写十六进制数 一 ...

  7. python爬虫期末复习

    python期末复习 选择题 以下选项中合法的是(A). A 爬取百度的搜索结果 B 爬取淘宝的商品数据 C 出售同学的个人信息 D 为高利贷提供技术服务 网站的根目录下有一个文件告诉爬虫哪些内容可以 ...

  8. 安装火狐浏览器报错找不到VCRUNTIME140_1.DLL

    产生原因参考及下载地址:https://cn.dll-files.com/vcruntime140_1.dll.html vcruntime140_1.dll 相关的错误可能源于多种不同原因.比如,错 ...

  9. Apache设置虚拟机端口

    Apache虚拟机设置端口,以45184端口为例httpd-vhosts.conf文件NameVirtualHost *:45184<VirtualHost *:45184>    Doc ...

  10. [BUUCTF]PWN——pwnable_hacknote

    pwnable_hacknote 附件 步骤: 例行检查,32位程序,开启了nx和canary保护 本地试运行看一下大概的情况,熟悉的堆的菜单 32位ida载入 add() gdb看一下堆块的布局更方 ...