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. centos6.5安装crmsh

    CentOS默认没有crmsh的yum源,因此可以借用OpenSUSE的源(OpenSUSE的包也是rpm). 操作步骤很简单首先先进入yum源的安装目录,下载repo配置文件,(返回原工作目录,)执 ...

  2. SQL进阶系列之4HAVING字句的力量

    写在前面 SQL是面向集合的语言,与面向过程和面向对象语言都不一样 寻找缺失的编号 /* 寻找缺失的编号 */ CREATE TABLE SeqTbl (seq INTEGER PRIMARY KEY ...

  3. java 的任意进制间转换

    直接上代码: public class Main { public static void main(String[] args) { // TODO Auto-generated method st ...

  4. ARTS-week1

    Algorithm 小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的 ...

  5. 【Selenium-WebDriver实战篇】java测试使用HttpClient debug日志关闭

    在上一篇设置完Tess4J之后,引用jar包之前,我的日志体系一直是只出现info级别的,但是引用之后出现很多httpClient的请求. 于是网上调查了下,可以通过代码实现,就在入口程序增加该部分代 ...

  6. Vue --- 基础练习

    1.有红,黄,蓝三个按钮,以及一个矩形框,点击不同的按钮,矩形框的颜色会被切换为指定的颜色 <!DOCTYPE html> <html lang="zh"> ...

  7. volatile 关键词

    volatile 关键字指示一个字段可以由多个同时执行的线程修改. 出于性能原因,编译器,运行时系统甚至硬件都可能重新排列对存储器位置的读取和写入. 声明了 volatile 的字段不进行这些优化.这 ...

  8. 2019.11.30 Mysql查询知识

    不等于:<> 判断为空的条件:null和空格(空字符串) 判断是否为null:xxxx  is  not  null    /    xxxx   is   null 判断null: SE ...

  9. WinDbg常用命令系列---内存数据显示和对应符号显示d*s(dds、dps、dqs)

    命令dds, dps,  dqs显示给定范围内的内存内容.假定该内存是符号表中的一系列地址.相应的符号也会显示出来. dds [Options] [Range] dqs [Options] [Rang ...

  10. Windbg命令的语法规则系列(一)

    本文介绍使用调试器命令必须遵循的语法规则.使用Windbg调试时,应遵守以下一般语法规则: 您可以在命令和参数中使用大小写字母的任意组合,除非在本节的主题中特别指出. 可以用一个或多个空格或逗号(,) ...