classSolution(object): defsummaryRanges(self, nums): """ :type nums: List[int] :rtype: List[str] """ # 長度為 0 或 1 之特例 iflen(nums) == 0: return [] eliflen(nums) == 1: return [str(nums[0])] ret = [] pre = -1 for c in nums: if c+1notin nums: if pre == -1: ret.append(str(c)) else: ret.append(str(pre)+"->"+str(c)) pre = -1 else: if pre == -1: pre = c
if pre == nums[-1]: ret.append(str(c)) elif pre != -1: ret.append(str(pre)+"->"+str(nums[-1])) return ret