Given a sorted integer array without duplicates, return the summary of its ranges.

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.

Java:

class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list=new ArrayList();
if(nums.length==1){
list.add(nums[0]+"");
return list;
}
for(int i=0;i<nums.length;i++){
int a=nums[i];
while(i+1<nums.length&&(nums[i+1]-nums[i])==1){
i++;
}
if(a!=nums[i]){
list.add(a+"->"+nums[i]);
}else{
list.add(a+"");
}
}
return list;
}
}  

Python:

class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
ranges = []
if not nums:
return ranges start, end = nums[0], nums[0]
for i in xrange(1, len(nums) + 1):
if i < len(nums) and nums[i] == end + 1:
end = nums[i]
else:
interval = str(start)
if start != end:
interval += "->" + str(end)
ranges.append(interval)
if i < len(nums):
start = end = nums[i] return ranges  

Python:

class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
""" ranges = []
for n in nums:
if not ranges or n > ranges[-1][-1] + 1:
ranges += [],
ranges[-1][1:] = n, return ['->'.join(map(str, r)) for r in ranges]  

Python:

class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
ranges, r = [], []
for n in nums:
if n-1 not in r:
r = []
ranges += r,
r[1:] = n,
print r, ranges
return ['->'.join(map(str, r)) for r in ranges]

C++:

class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
int i = 0, n = nums.size();
while (i < n) {
int j = 1;
while (i + j < n && nums[i + j] - nums[i] == j) ++j;
res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
i += j;
}
return res;
}
};

C++:

class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
const int size_n = nums.size();
vector<string> res;
if ( 0 == size_n) return res;
for (int i = 0; i < size_n;) {
int start = i, end = i;
while (end + 1 < size_n && nums[end+1] == nums[end] + 1) end++;
if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else res.push_back(to_string(nums[start]));
i = end+1;
}
return res;
}
};

  

类似题目: 

[LeetCode] 163. Missing Ranges 缺失区间

  

All LeetCode Questions List 题目汇总

最新文章

  1. Redis实战阅读笔记——第二章
  2. BZOJ4590: [Shoi2015]自动刷题机
  3. linux 下 jdk+tomcat+mysql 的 jsp 环境搭建
  4. 本地虚拟机中匿名ftp上传文件失败的问题
  5. Python图片与其矩阵数据互相转换
  6. java正则表达式应用--验证字符串是否为数字(转载)
  7. c语言,strcpy
  8. Quartz1.8.5例子(四)
  9. java 项目request.getParameter(&quot;&quot;)接收不到值
  10. kafka学习(二)-zookeeper集群搭建
  11. 浏览器本地储存方式有哪些?cookie、localStorage、sessionStorage
  12. UVALive3882-And Then There Was One-约瑟夫问题-递推
  13. CDOJ 1965 连通域统计【DFS】
  14. python 爬虫数据存入csv格式方法
  15. Tree - Decision Tree with sklearn source code
  16. PL/SQL之DBMS_SQL程序包使用(1)(学习笔记)
  17. T Fiddler 教程 _转
  18. 【游记】CTSC&amp;APIO2017
  19. http测试工具ab
  20. bzoj 3192 删除物品

热门文章

  1. 由Catalan数所引出的
  2. Linux TTY介绍
  3. ARTS-week2
  4. 在linux中使用Sqlplus命令登录MySQL,查看表并设置行数和宽度,使其正常显示
  5. mybatis的注意事项一
  6. 关于我&amp;声明
  7. 学习Spring-Data-Jpa(十五)---Auditing与@MappedSuperclass
  8. LOJ P10016 灯泡 题解
  9. box-sizing 盒子模型
  10. 12-ESP8266 SDK开发基础入门篇--PWM,呼吸灯