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. Shiro workshop

    吃掉<Shiro教程>的精要部分,Go ahead!

  2. 版本控制 - Git

    此篇blog只是对http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 研读后的总结,还 ...

  3. linux命令集——<一>目录处理命令

    1.ls,命令,显示当前目录下文件 命令 所在路径 功能描述 执行权限 选项 说明 ls /bin/ls 显示目录文件 所有用户可用 -a 显示所有文件,包括隐藏文件         -l 显示详细信 ...

  4. NYOJ-205 求余数 AC 分类: NYOJ 2014-02-02 12:30 201人阅读 评论(0) 收藏

    这题目看一眼以为难度评级出错了,只是一个求余数的题目,,后来才发现,位数小于百万位,,,我还以为是大小小于百万呢,所以借鉴了另一大神的代码, 用大数,重点是同余定理: (a+b)mod m=((a m ...

  5. Sqli-labs less 64

    Less-64 此处的sql语句为 $sql="SELECT * FROM security.users WHERE id=(($id)) LIMIT 0,1"; 示例payloa ...

  6. 7 天玩转 ASP.NET MVC — 第 6 天

    目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 欢迎来到第六天的 MVC 系列学习中.希望你在阅读此篇文章的时候,已经学习了前五天的内容,这也是第 ...

  7. MVC 中 Razor 无限分类的展示

    在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上.可能本人水平有限,实在是不会用. 那我就用最简单爆力的办法来做. Model: public class NewsCa ...

  8. POJ 1947 Rebuilding Roads(树形DP)

    题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...

  9. 浅谈mysql中varchar(m)与char(n)的区别与联系

    mysql建表长度的限制 在mysql建表时,出现以下报错信息: 错误一:行大小过大,所使用的表这种类型的最大的行大小,不算BLOB类型,是65535.(这是我翻译的)    原因是MySQL在建表的 ...

  10. Photoshop支持ico输出

    1.Photoshop支持ico格式插件下载:ICOFormat.8bi 2.把文件放到ps安装目录:xxx/Plug-ins/File Formats 下 3.重启下ps