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"].

分析:题意为 给定一个有序整型数组,返回总结区间,具体来说就是找出连续的序列,然后首尾两个数字之间用个“->"来连接,那么只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续往下遍历,如果不是了,我们还要判断此时是一个数还是一个序列,一个数直接存入结果,序列的话要存入首尾数字和箭头“->"。我们需要两个变量i和j,其中i是连续序列起始数字的位置,j是连续数列的长度,当j为1时,说明只有一个数字,若大于1,则是一个连续序列。

代码如下:

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; //++j 加了再用,这也是后面j<=1,i+j-1的原因
res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
i += j;
}
return res;
}
};

第一次提交时,粗心将nums[i + j] - nums[i] == j中的=少写了一个,成了赋值运算符,所以出错:lvalue required as left operand of assignment(左值要求作为转让的左操作数)--------要仔细,要仔细,要仔细

其他解法:

 class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int len = nums.size(), i, start ;
vector<string> res; for(i=1, start=0;i<=len;++i)
{
if(i==len || (nums[i-1] + 1)!=nums[i])
{ // the current range is finished or it reaches the end of vector, write back
res.push_back(((i-1) ==start)? to_string(nums[start]): (to_string(nums[start])+"->"+to_string(nums[i-1])));
start = i;
}
}
return res;
}
};

  

 

leetcode: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 OJ:Summary Ranges(概括区间)

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

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

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

  4. Java for LeetCode 228 Summary Ranges

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

  5. (easy)LeetCode 228.Summary Ranges

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

  6. Java [Leetcode 228]Summary Ranges

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

  7. C#解leetcode 228. Summary Ranges Easy

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

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

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

  9. LeetCode(228) Summary Ranges

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

随机推荐

  1. 对LVS DR模式的理解

    Client向vip发请求,lvs接收 Src mac Dst mac type … Src ip Src port Dst ip Dst port … CRC Mac1 Mac2 … … 192.1 ...

  2. [百度空间] [原] 全局operator delete重载到DLL

    由于很久没有搞内存管理了,很多细节都忘记了今天项目要用到operator delete重载到DLL,发现了问题,网上搜索以后,再对比以前写的代码,发现了问题:原来MSVC默认的operator new ...

  3. boost 相关

    编译boost: 1.打开Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prom ...

  4. SQLServer中查询的数字列前面补0返回指定长度的字符串

    SQLServer中查询的数字列前面补0返回指定长度的字符串: 如: 角本如下: /****** Script for SelectTopNRows command from SSMS ******/ ...

  5. Mozilla推荐的CSS属性书写顺序及命名规则

    传说中的Mozilla推荐 /* mozilla.org Base Styles * maintained by fantasai */ /* Suggested order: * display * ...

  6. 利用excel数据透视表实现快速统计相关数据

    昨天ytkah在做数据报表时需要做一些具体统计数字:公司每天都有人申请铅笔.笔记本等一些文具用品,现在想要统计每天申请铅笔多少支.笔记本多少本,如下图所示,这个要如何实现呢? excel数据透视表怎么 ...

  7. list<T> 自定义比较器进行排序

    今天在研究List<T> 集合如何排序,我试过很多,但是都不行,然后看到msdn中的这个比较器排序,自己测试了代码,No Problem.给大家分享一下. 类型 T 的默认比较器按如下方式 ...

  8. nested pop animation can result in corrupted navigation bar

    nested pop animation can result in corrupted navigation barFinishing up a navigation transition in a ...

  9. Visual Studio 2015支持为Linux构建应用

    点这里 微软著名的集成开发环境有可能是首次在其产品页提及了竞争对手Linux.Visual Studio 2015的页面声称,“Build for iOS, Android, Windows devi ...

  10. Android中的Context详解

    前言:本文是我读<Android内核剖析>第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书. 大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友---- ...