当前位置:网站首页>力扣(LeetCode)163. 缺失的区间(2022.06.12)
力扣(LeetCode)163. 缺失的区间(2022.06.12)
2022-06-13 07:34:00 【ChaoYue_miku】
给定一个排序的整数数组 nums ,其中元素的范围在 闭区间 [lower, upper] 当中,返回不包含在数组中的缺失区间。
示例:
输入: nums = [0, 1, 3, 50, 75], lower = 0 和 upper = 99,
输出: [“2”, “4->49”, “51->74”, “76->99”]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/missing-ranges
方法一:模拟、一次遍历
Python3提交内容:
class Solution:
def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
# 添加终止边界
nums.append(upper + 1)
ans = []
last = lower - 1
for num in nums:
if num - last > 2:
ans.append(str(last+1) + '->' + str(num-1))
elif num - last == 2:
ans.append(str(last+1))
last = num
return ans
边栏推荐
- Considerations for using redis transactions
- redis-5. Redis' RDB, fork, copyonwrite, AOF, RDB & AOF are mixed
- China phosphate market in-depth analysis and investment prospect forecast report 2022-2028
- 25 | 冒险和预测(四):今天下雨了,明天还会下雨么?
- [hard copy] core differences among dirty reading, non repeatable reading and unreal reading scenarios
- Login registration
- Sorting of numbers and strings
- Simple use of logs
- About database: pgadmin4 editing SQL window
- Interview questions must be asked - Optimization of large table Pagination
猜你喜欢

Un des backtraders du cadre de quantification lit l'analyseur

powerdisgner逆向生成oracle数据模型

redis-4. Redis' message subscription, pipeline, transaction, modules, bloom filter, and cache LRU

How app inventor accesses resource files in assets directory

MySQL Gtid_ Executed and gtid_ Purged modification time

redis-7. Redis master-slave replication, cap, Paxos, cluster sharding cluster 02

Redis Cluster - the underlying principle of cluster execution commands

RT thread simulator lvgl control: slider control

Compare advantages and disadvantages of DFS and BFS and name vocabulary

19 | 建立数据通路(下):指令+运算=CPU
随机推荐
20 | 面向流水线的指令设计(上):一心多用的现代CPU
11.29 Li Kou swipes questions every day
Redis learning journey - cache exceptions (CACHE penetration, cache avalanche, cache breakdown)
The management practice of leading enterprises has proved that what is the core of sustainable development of enterprises?
redis-3. Redis list, set, hash, sorted_ set、skiplist
25个国内外文献数据库
Redis' underlying data structure -- SDS
理财产品连续几天收益都是零是怎么回事?
Sorting of numbers and strings
[log4j2 log framework] modify dump log file permissions
mysql中时间字段 比较时间大小
Export chrome plug-ins and import local plug-ins to chrome
Deploy RDS service
Redis learning journey master-slave replication
How is it that the income of financial products is zero for several consecutive days?
TCP协议的三次握手过程和四次挥手过程以及为什么要这样? ------一二熊猫
How app inventor accesses resource files in assets directory
IDS persistence ---rdb
【splishsplash】重复输出splashsurf的脚本
One article of quantitative framework backtrader read analyzer