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 题目汇总

[LeetCode] 228. Summary Ranges 总结区间的更多相关文章

  1. LeetCode 228. Summary Ranges (总结区间)

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  2. [leetcode]228. Summary Ranges区间统计

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  3. C#解leetcode 228. Summary Ranges Easy

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

  4. Java for LeetCode 228 Summary Ranges

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

  5. LeetCode(228) Summary Ranges

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

  6. (easy)LeetCode 228.Summary Ranges

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

  7. Java [Leetcode 228]Summary Ranges

    题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...

  8. 228 Summary Ranges 汇总区间

    给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...

  9. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

随机推荐

  1. python——使用xlwing库进行Excel操作

    Excel是现在比不可少的数据处理软件,python有很多支持Excel操作的库,xlwing就是其中之一. xlwings的安装 xlwings库使用pip安装: 在控制台输入 pip instal ...

  2. python开发笔记-变长字典Series的使用

    Series的基本特征: 1.类似一维数组的对象 2.由数据和索引组成 import pandas as pd >>> aSer=pd.Series([1,2.0,'a']) > ...

  3. postgres —— 分组集与部分聚集

    创建表 create table t_oil ( region text, country text, year text, production int, comsumption int ) 导入数 ...

  4. Hibernate缓存简介和对比、一级缓存、二级缓存详解

    一.hibernate缓存简介 缓存的范围分为3类:  1.事务范围(单Session即一级缓存)     事务范围的缓存只能被当前事务访问,每个事务都有各自的缓存,缓存内的数据通常采用相互关联的对象 ...

  5. JSON和计算机网络的个人总结

    JSON JSON是什么? JSON:JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于ECMAScript (欧洲计算机协会制定的js规范) ...

  6. LeetCode 841. Keys and Rooms

    原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...

  7. ssh集成

    导入pom依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  8. WinDbg常用命令系列---日志操作相关命令log*

    .logopen (Open Log File) .logopen命令将事件和命令的副本从调试器命令窗口发送到新的日志文件. .logopen [Options] [FileName] .logope ...

  9. jedis的连接池

    1.需要先打开虚拟机,并开启Linux系统的端口号:6379: 其中,第一行代码为修改字符编码格式,解决SSH中文乱码问题. 2.开启redis: 3.利用连接池实现数据的存取: (1)代码实现: i ...

  10. PyCharm中Dictionary与Python package的区别

    Dictionary Dictionary在pycharm中就是一个文件夹,放置资源文件,对应于在进行JavaWeb开发时用于放置css/js文件的目录,或者说在进行物体识别时,用来存储背景图像的文件 ...