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

Example 1:

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

Example 2:

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

Java:

  1. class Solution {
  2. public List<String> summaryRanges(int[] nums) {
  3. List<String> list=new ArrayList();
  4. if(nums.length==1){
  5. list.add(nums[0]+"");
  6. return list;
  7. }
  8. for(int i=0;i<nums.length;i++){
  9. int a=nums[i];
  10. while(i+1<nums.length&&(nums[i+1]-nums[i])==1){
  11. i++;
  12. }
  13. if(a!=nums[i]){
  14. list.add(a+"->"+nums[i]);
  15. }else{
  16. list.add(a+"");
  17. }
  18. }
  19. return list;
  20. }
  21. }  

Python:

  1. class Solution:
  2. # @param {integer[]} nums
  3. # @return {string[]}
  4. def summaryRanges(self, nums):
  5. ranges = []
  6. if not nums:
  7. return ranges
  8.  
  9. start, end = nums[0], nums[0]
  10. for i in xrange(1, len(nums) + 1):
  11. if i < len(nums) and nums[i] == end + 1:
  12. end = nums[i]
  13. else:
  14. interval = str(start)
  15. if start != end:
  16. interval += "->" + str(end)
  17. ranges.append(interval)
  18. if i < len(nums):
  19. start = end = nums[i]
  20.  
  21. return ranges  

Python:

  1. class Solution(object):
  2. def summaryRanges(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[str]
  6. """
  7.  
  8. ranges = []
  9. for n in nums:
  10. if not ranges or n > ranges[-1][-1] + 1:
  11. ranges += [],
  12. ranges[-1][1:] = n,
  13.  
  14. return ['->'.join(map(str, r)) for r in ranges]  

Python:

  1. class Solution(object):
  2. def summaryRanges(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[str]
  6. """
  7. ranges, r = [], []
  8. for n in nums:
  9. if n-1 not in r:
  10. r = []
  11. ranges += r,
  12. r[1:] = n,
  13. print r, ranges
  14. return ['->'.join(map(str, r)) for r in ranges]

C++:

  1. class Solution {
  2. public:
  3. vector<string> summaryRanges(vector<int>& nums) {
  4. vector<string> res;
  5. int i = 0, n = nums.size();
  6. while (i < n) {
  7. int j = 1;
  8. while (i + j < n && nums[i + j] - nums[i] == j) ++j;
  9. res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
  10. i += j;
  11. }
  12. return res;
  13. }
  14. };

C++:

  1. class Solution {
  2. public:
  3. vector<string> summaryRanges(vector<int>& nums) {
  4. const int size_n = nums.size();
  5. vector<string> res;
  6. if ( 0 == size_n) return res;
  7. for (int i = 0; i < size_n;) {
  8. int start = i, end = i;
  9. while (end + 1 < size_n && nums[end+1] == nums[end] + 1) end++;
  10. if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
  11. else res.push_back(to_string(nums[start]));
  12. i = end+1;
  13. }
  14. return res;
  15. }
  16. };

  

类似题目: 

[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. anyproxy学习1-windows平台安装和抓手机app上https请求

    前言 做接口测试肯定离不开抓包,目前比较流行的抓包工具是fiddler和charles,相信并不陌生.这里介绍一个阿里公司研发的一个抓包神器,只需打开web页面,就能抓到手机app上的http和htt ...

  2. Alpha版本1之后的成绩汇总

    作业要求 1.作业内容: 作业具体要求及评分标准的链接 2.评分细则 •给出开头和团队成员列表(10’) •给出发布地址以及安装手册(20’) •给出测试报告(40’) •给出项目情况总结(30’) ...

  3. mybatis从入门到精通

    https://www.cnblogs.com/zwwhnly/p/11104020.html

  4. C++中的hash_map和map的区别

    hash_map和map的区别在哪里?构造函数.hash_map需要hash函数,等于函数:map只需要比较函数(小于函数). 存储结构.hash_map采用hash表存储,map一般采用红黑树(RB ...

  5. maven的目录

    maven目录主要分为: src/main/java:项目主体源代码目录 src/main/resources:项目主体源代码所需资源目录 src/test/java:测试代码目录(测试代码不会被打包 ...

  6. 海康相机开发(1) SDK安装和开发

    1.1 安装包获取 从官网下载最新版本的MVS安装包,支持Windows xp.Windows 7.Windows 8.Windows 10的32和64位系统.安装过程默认即可. 官网下载链接:htt ...

  7. (尚021)Vue_eslint编码规范检查

    1.eslint 1.1说明 1)ESLint是一个代码规范检查工具 2)它定义了很多特定的规则,一旦你的代码违背了某一规则,eslint会做出非常有用的提示 3)官网:http://eslint.o ...

  8. Noip 2017 题目整理

    目录 2017Noip: 小凯的疑惑 时间复杂度 逛公园 奶酪 宝藏(50fen) 列队(QAQ不会,以后再研究吧) 2017Noip: 小凯的疑惑 题目描述 小凯手中有两种面值的金币,两种面值均为正 ...

  9. 查全率(Recall),查准率(Precision),灵敏性(Sensitivity),特异性(Specificity),F1,PR曲线,ROC,AUC的应用场景

    之前介绍了这么多分类模型的性能评价指标(<分类模型的性能评价指标(Classification Model Performance Evaluation Metric)>),那么到底应该选 ...

  10. C Primer Plus--C预处理器和C库(1)

    目录 预处理符号 明显常量 #define 编译程序之前,先由预处理器检查程序(因此称为预处理器).根据程序中使用的预处理器指令,预处理用符号缩略语所代表的内容替换程序中的缩略语. 预处理器可以根据你 ...