Summary Ranges
2015-07-01 11:22
197 查看
题目:
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given
解题思路:
class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
ans = []
i = 0
while i<len(nums):
if i<len(nums)-1 and 1==nums[i+1]-nums[i]:
start = nums[i]
j = i+1
while(j<len(nums) and nums[j]-nums[i]==1):
i += 1
j += 1
end = nums[j-1]
ans.append(str(start)+"->"+str(end))
i = j
else:
ans.append(str(nums[i]))
i += 1
return ans
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given
[0,1,2,4,5,7], return
["0->2","4->5","7"].
解题思路:
class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
ans = []
i = 0
while i<len(nums):
if i<len(nums)-1 and 1==nums[i+1]-nums[i]:
start = nums[i]
j = i+1
while(j<len(nums) and nums[j]-nums[i]==1):
i += 1
j += 1
end = nums[j-1]
ans.append(str(start)+"->"+str(end))
i = j
else:
ans.append(str(nums[i]))
i += 1
return ans
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- 解决 ECSHOP v273 产品详情页面评论不显示的问题
- uitableview中刷新单个section和row
- WEB前端网页返回代码常见值
- django 实现 group by
- 编译安装PHP-GD
- Android 百分比布局库(percent-support-lib) 解析与扩展
- Spring2.5学习4.1_AOP
- Android 判断某一个类是否存在任务栈里面
- Be JSON (Just Make JSON Easy!) 什么是JSON
- word 操作
- Oracle:environment variable "PATH" does not exceed the recommended length
- java 集合(list、set、map)的特点
- iOS 客户端rceipt验证代码
- Handler 引起的内存泄露之解决方法WeakReference
- PC 端两端对齐 中间等分
- Python学习笔记3_列表和元组
- IOS Protocol与Delegate详解(一)
- swap解密
- WebRTC for iOS 下载