题目地址: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. mysql proxy 数据库读写分离字符集乱码

    mysql proxy 数据库读写分离字符集乱码 解决办法 在对应配置后端数据库服务器的配置.cnf中加入如下代码 init-connect='SET NAME UTF8' skip-characte ...

  2. 在Linux下搭建nRF51822的开发烧写环境(makefile版)

    http://www.qingpingshan.com/m/view.php?aid=394836

  3. 错误笔记: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to lock the administration di

    亲测可用 --jack alexander@alexander-virtual-machine:~$ sudo apt-get install -y httpdE: Could not get loc ...

  4. C 语言中求中间数时候防止溢出方法

    当使用二分法时候,注意 mid = left + (right - left) / 2; 这句代码,可以防止溢出!!,千万不能写成 mid = (left + right) / 2 这样写的话,当数字 ...

  5. C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘myString’

    先看代码(不想看代码可以直接看代码后的问题描述) //header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #inc ...

  6. What all is inherited from parent class in C++?

    派生类可以从基类中继承: (1)基类中定义的每个数据成员(尽管这些数据成员在派生类中不一定可以被访问): (2)基类中的每个普通成员函数(尽管这些成员函数在派生类中不一定可以被访问): (3)The ...

  7. Oracle学习笔记(1)

    折腾了好久 终于把oracle安装成功了.小兴奋下. 创建了一个数据库 dabook. run--> Services.msc查看服务: 可以看到DABOOK的服务已启动. 1,sys用户 在c ...

  8. 【编程思想】【设计模式】【创建模式creational】建造者模式builder

    Python版 https://github.com/faif/python-patterns/blob/master/creational/builder.py #!/usr/bin/python ...

  9. mybatis联合查询

    1.有学生实体 @Component @Scope("prototype") public class StudentInfo { private Integer studentI ...

  10. 关于tensorflow无法使用gpu

    python3.6 无法使用tensorflow gpu 环境名称 test1 在控制台里进入环境 conda activate test1 使用python python 查看gpu能否使用 pri ...