Posted on 2023-06-12 17:37
Uriel 閱讀(37)
評論(0) 編輯 收藏 引用 所屬分類:
模擬 、
閑來無事重切Leet Code
給定一列已排序且各不相同的數列,輸出固定格式的連續數(參考了DIscussion如何python3實現格式化輸出)
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
1 #228
2 #Runtime: 51 ms (Beats 21.61%)
3 #Memory: 16.2 MB (Beats 64.73%)
4
5 class Solution:
6 def summaryRanges(self, nums: List[int]) -> List[str]:
7 ans = []
8 for i, x in enumerate(nums):
9 if ans and ans[-1][1] == x - 1:
10 ans[-1][1] = x
11 else:
12 ans.append([x, x])
13 return [f'{x}->{y}' if x != y else f'{x}' for x, y in ans]