엔지니어 게시판
LeetCode 솔루션 분류

[Easy - wk7 - Q3] 163. Missing Ranges

컨텐츠 정보

본문

163. Missing Ranges 


You are given an inclusive range [lower, upper] and a sorted unique integer array nums, where all elements are in the inclusive range.

A number x is considered missing if x is in the range [lower, upper] and x is not in nums.

Return the smallest sorted list of ranges that cover every missing number exactly. That is, no element of nums is in any of the ranges, and each missing number is in one of the ranges.

Each range [a,b] in the list should be output as:

  • "a->b" if a != b
  • "a" if a == b

 

Example 1:

Input: nums = [0,1,3,50,75], lower = 0, upper = 99
Output: ["2","4->49","51->74","76->99"]
Explanation: The ranges are:
[2,2] --> "2"
[4,49] --> "4->49"
[51,74] --> "51->74"
[76,99] --> "76->99"

Example 2:

Input: nums = [-1], lower = -1, upper = -1
Output: []
Explanation: There are no missing ranges since there are no missing numbers.

 

Constraints:

  • -109 <= lower <= upper <= 109
  • 0 <= nums.length <= 100
  • lower <= nums[i] <= upper
  • All the values of nums are unique.

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 45 ms, faster than 42.04% of Python3 online submissions for Missing Ranges.
Memory Usage: 13.9 MB, less than 83.76% of Python3 online submissions for Missing Ranges.

class Solution:
    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
        res = []

        nums = [lower-1]+nums+[upper+1]
        for idx, num in enumerate(nums[:-1]):
            start, end = num + 1, nums[idx+1]-1
            
            if start > end: continue
            elif start == end: res.append(str(start))
            else: res.append(str(start)+"->"+str(end))
                
        
        return res
전체 404 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


알림 0