Advertisement
kosievdmerwe

Untitled

Feb 27th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. class Solution:
  2.     def summaryRanges(self, nums: List[int]) -> List[str]:
  3.         ans = []
  4.         cur_range = None
  5.        
  6.         def pop_cur_range() -> None:
  7.             nonlocal ans, cur_range
  8.             if cur_range is None:
  9.                 return
  10.            
  11.             if cur_range[0] == cur_range[1]:
  12.                 ans.append(f"{cur_range[0]}")
  13.             else:
  14.                 ans.append(f"{cur_range[0]}->{cur_range[1]}")
  15.             cur_range = None
  16.        
  17.         for n in nums:
  18.             if cur_range is not None and cur_range[1] + 1 != n:
  19.                 pop_cur_range()
  20.             if cur_range is None:
  21.                 cur_range = [n, n]
  22.             cur_range[1] = n
  23.         pop_cur_range()
  24.         return ans
  25.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement