163. Missing Ranges
package LeetCode_163 /** * 163. Missing Ranges * (Prime) *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"] * */ class Solution { /* * solution: Two pointer, lower_ and upper_, keep checking lower_ and update it if need; * Time complexity:O(n), Space complexity:O(1) * */ fun getRanges(nums: IntArray, lower: Int, upper: Int): List<String> { val result = ArrayList<String>() //use long to avoid stack overflow var lower_ = lower.toLong() val upper_ = upper.toLong() //keep checking lower for (num in nums) { if (num.toLong() == lower_) { //increasing the lower lower_++ } else if (lower_ < num) { //for example: lower_ is 2, num is 3 if (lower_ + 1 == num.toLong()) { result.add(lower_.toString()) } else { //for example: lower_ is 4, num is 50 result.add("$lower_->${num - 1}") } //update lower lower_ = num.toLong() + 1 } } //check the upper if (lower_ == upper_) { result.add(lower_.toString()) } else { result.add("$lower_->$upper_") } return result } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2019-09-07 48. Rotate Image