LC 163. Missing Ranges 【lock, hard】
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"]
这题应该是实现的细节处理,最大整数,最小整数比较难,也是调了很久的corner case才调出来的。
好不容易Accept了,自己写的还是有点惨。
还要注意
if(lower++ == something) something;
不管判断对不对,lower都会自增的,要当心。
AC第一版
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
vector<string> ret;
vector<int> processed;
for(int i=; i<nums.size(); i++){
if(processed.empty() || processed.back() != nums[i]){
processed.push_back(nums[i]);
}
}
nums.clear();
//for(auto i : processed) nums.push_back(i); for(auto i : processed) cout << i << " ";
cout << endl;
nums = processed;
if(lower == upper) {
if(!nums.empty()) return ret;
else ret.push_back(to_string(lower));
return ret;
}
if(nums.empty()){
ret.push_back(to_string(lower) + "->" + to_string(upper));
return ret;
}
for(int i=; i<nums.size(); i++){
//cout << lower << "and" << nums[i] << endl;
if (lower == nums[i]) {
lower++;
continue;
} if(lower+ == nums[i]) ret.push_back(to_string(lower));
else ret.push_back(to_string(lower) + "->" + to_string(nums[i]-));
if(nums[i] == ( << )-) return ret;
lower = nums[i]+;
}
//cout << lower << upper << endl;
if(lower > upper) return ret;
if(lower == upper) ret.push_back(to_string(lower));
else ret.push_back(to_string(lower) + "->" + to_string(upper));
return ret;
}
AC 第二版
简化了一些代码
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
vector<string> ret;
vector<int> processed;
// filter duplicate
for(auto val : nums) {
if(processed.empty() || processed.back() != val) processed.push_back(val);
}
nums.clear();
nums = processed;
// for(auto i : processed) cout << i << " ";
// cout << endl;
// corner case 1: lower == upper
if(lower == upper) {
if(nums.empty()) ret.push_back(to_string(lower));
return ret;
}
for(int i=; i<nums.size(); i++){
if (lower != nums[i]) {
if(lower+ == nums[i]) ret.push_back(to_string(lower));
else ret.push_back(to_string(lower) + "->" + to_string(nums[i]-));
}
if(nums[i] == ( << )-) return ret;
lower = nums[i]+;
}
//cout << lower << upper << endl;
if(lower == upper) ret.push_back(to_string(lower));
else if(lower < upper) ret.push_back(to_string(lower) + "->" + to_string(upper));
return ret;
}
LC 163. Missing Ranges 【lock, hard】的更多相关文章
- LC 660. Remove 9 【lock, hard】
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- LC 656. Coin Path 【lock, Hard】
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- 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 ...
- LeetCode 163. Missing Ranges (缺失的区间)$
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [leetcode]163. Missing Ranges缺失范围
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
随机推荐
- 第十五章、Python多线程同步锁,死锁和递归锁
目录 第十五章.Python多线程同步锁,死锁和递归锁 1. 引子: 2.同步锁 3.死锁 引子: 4.递归锁RLock 原理: 不多说,放代码 总结: 5. 大总结 第十五章.Python多线程同步 ...
- linux之网络命令
本文整理了在实践过程中使用的Linux网络工具,这些工具提供的功能非常强大,我们平时使用的只是冰山一角,比如lsof.ip.tcpdump.iptables等. 本文不会深入研究这些命令的强大用法,因 ...
- OSG入坑之路[转]
转载自:https://segmentfault.com/a/1190000010506374?utm_source=tag-newest osg插件原理:https://blog.csdn.net/ ...
- Linux工具之watch
watch watch 监测一个命令的运行结果 -n 指定间隔的时间 -d watch会高亮显示变化的区域. -t 会关闭watch命令在顶部的时间间隔, ...
- zabbix 3.2.2 server端(源码包)安装部署 (一)
环境准备: 操作系统 CentOS 6.8 2.6.32-642.11.1.el6.x86_64 zabbix server 172.16.10.150 zabbix agent 172.16.10. ...
- Linux下计划任务:crontab 命令的权限说明
Linux下的计划任务: 使用crontab命令来执行调度,在 Linux 下可以通过创建文件 /etc/cron.allow 或者 /etc/cron.deny 来控制权限,如果 /etc/cron ...
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期
Interrupt学习: 在jdk中关于interrupt相关方法有三个,如下: 关于上面的疑问会在稍后进行阐述滴,下面看代码: 编译运行: 应该说是t线程为啥在被打断之后没有退出,还是在运行状态,这 ...
- netty-4.客户端与服务端心跳
(原) 第四篇,客户端与服务端心跳 心跳事件有三种,读空闲,写空闲,读写空闲,定义在了IdleState枚举类中,分别为READER_IDLE,WRITER_IDLE,ALL_IDLE 服务端: ma ...
- MySQL BinLog Server 搭建实战
一.MySQL Binlog server 介绍 MySQL Binlog Server: 它使用 mysqlbinlog 命令以 daemon 进程的方式模拟一个 slave 的 IO 线程与主库连 ...
- 并发编程-Java内存模型
将之前看过的关于并发编程的东西总结记录一下,本文简单记录Java内存模型的相关知识. 1. 并发编程两个关键问题 并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步. (1)在命令式 ...