当前位置:网站首页>leetcode:163 缺失的区间
leetcode:163 缺失的区间
2022-08-03 02:04:00 【OceanStar的学习笔记】
题目来源
题目描述
给定一个排序的整数数组 nums ,其中元素的范围在 闭区间 [lower, upper] 当中,返回不包含在数组中的缺失区间。
示例:
输入: nums = [0, 1, 3, 50, 75], lower = 0 和 upper = 99,
输出: [“2”, “4->49”, “51->74”, “76->99”]
题目解析
- 数组已经排序,但是并不能二分,因为要求所有的缺失区间
- 因此一次遍历
class Solution {
// 生成"lower->upper"的字符串,如果lower==upper,只用生成"lower"
std::string miss(int lower, int upper){
std::string left = std::to_string(lower);
std::string right;
if(upper > lower){
right = "->" + std::to_string(upper);
}
return left + right;
}
public:
std::vector<std::string> missing_ranges(vector<int>& nums, int low, int high) {
std::vector<std::string> ans;
for (int curr : nums) {
if(curr > low){
ans.push_back(miss(low, curr - 1));
low = curr + 1;
}else if(curr == low){
low = curr + 1;
}
if(curr == high){
return ans;
}
}
if(low <= high){
ans.push_back(miss(low, high));
}
return ans;
}
};
边栏推荐
- 【UE4】搭建局域网内VR直播 UE4.27
- vs studio install opencv environment
- 实现统一账号登录,sonarqube集成ldap
- 能添加任意贴图超级复布局的初级智能文本提示器(超级版)
- pytest:如何调用 pytest
- 什么样的存储服务,才能成为企业数字化创新“加速器”?
- 【静态类型和动态类型 编译检查和运行检查 Objective-C中】
- Incorrect datetime value: ‘2022-01-01‘ for function str_to_date
- 【云原生】灰度发布、蓝绿发布、滚动发布、灰度发布解释
- 236. The binary tree in recent common ancestor
猜你喜欢

国标GB28181协议EasyGBS平台项目现场通知消息过多导致系统卡顿该如何解决?

LVS-NAT模式【案例实验】

为什么要使用 playwright 做浏览器自动化测试?

236. The binary tree in recent common ancestor

radio button、qss文件环境配置

openCV第一篇

复杂多层布局的初级智能文本提示器

Wei Dongshan Digital Photo Frame Project Learning (5) Transplantation of libjpeg-turbo

.NET in-depth analysis of the LINQ framework (four: IQueryable, IQueryProvider interface details)

The Multiversity 的 “非常重要的生命体” NFT 推出
随机推荐
【7.31】代码源 - 【矩阵操作】【宝箱】【New Stone Game】【等差数列】
能添加任意贴图超级复布局的初级智能文本提示器(超级版)
.NET in-depth analysis of the LINQ framework (four: IQueryable, IQueryProvider interface details)
Wireshark data capture and analysis of the transport layer protocol (TCP protocol)
自定义RunTimeException工具类
EasyGBS播放器优化:设备通道视频播放出现跳屏问题的修复
流程图(1)
一个循环,两个循环问题的思考及复现
程序员写代码日常 | 每日趣闻
lombok 下的@Builder和@EqualsAndHashCode(callSuper = true)注解
Interconversion between numpy PIL tensors
rancher集成ldap,实现统一账号登录
PHICOMM(斐讯)N1盒子 - Armbian5.77(Debian 9)配置自动连接WIFI无线网络
大厂标配 | 百亿级并发系统设计 | 学完薪资框框涨
236. The binary tree in recent common ancestor
新库上线 | CnOpenDataA股上市公司董监高信息数据
禁用token及无感知更新token功能实现
ldap创建公司组织、人员
qt opengl 使用不同的颜色绘制线框三角形
国标GB28181协议EasyGBS平台项目现场通知消息过多导致系统卡顿该如何解决?