728x90
class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
if not nums:
return []
start = nums[0]
end = nums[0]
ans = []
def beautify(a, b):
if a == b:
return str(a)
else:
return str(a) + "->" + str(b)
for i in range(1,len(nums)):
if (nums[i] - nums[i-1]) != 1:
ans.append(beautify(start,nums[i-1]))
start = nums[i]
ans.append(beautify(start, nums[-1]))
return ans
for 문 첫번째 if 문을 통해 연속되는 수가 끊기는 idx 기준으로 nums[i-1]이 end 지점이 된다.
start 지점을 update 해줘야하기 때문에 start = nums[i]가 된다
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Lowest Common Ancestor 파이썬 (tree) (0) | 2021.04.07 |
---|---|
[LeetCode] Palindrome Linked List 파이썬 (0) | 2021.04.06 |
[LeetCode] Invert Binary Tree 파이썬(recursion) (0) | 2021.04.05 |
[LeetCode] Contains Duplicate II 파이썬 (dict) (0) | 2021.04.05 |
[LeetCode] Reverse Linked List 파이썬 (0) | 2021.04.05 |
댓글