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. VS Bug 当获取其他项目的代码时, F5 无法进入调试模式. 也不报错....

    在64位的机子下, 被获用的项目使用X86时会出现. 就会出现   F5 无法进入调试模式. 也不报错.... 打断点也没有用. 在不加入X86项目的代码时, 又可以运行..   解决方案:   检查 ...

  2. INSERT IGNORE 与INSERT INTO的区别

      INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以 ...

  3. vi编辑

    保存命令 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并退出vi :wq! 强制 ...

  4. win7修改hosts文件方法

    因权限的关系,导致无法修改,解决如下: hosts属性 --> 安全 --> 编辑权限 --> 完全控制,就可以修改该文件了.win7默认不能修改hosts文件.

  5. Linux Rsync

    一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...

  6. vc++ 获取当前用户名

    #include<afxwin.h> #include <stdio.h> int main(void) { char userName[MAX_PATH]; unsigned ...

  7. (转)約瑟夫問題的兩個O(log n)解法

    約瑟夫問題的兩個O(log n)解法 這個是學習編程時的一個耳熟能詳的問題了: n個人(編號爲0,1,...,n-1)圍成一個圈子,從0號開始依次報數,每數到第m個人,這個人就得自殺, 之後從下個人開 ...

  8. cf div2 239 D

    D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. Hungary(匈牙利算法)——二分图最大匹配

    在复习匈牙利算法的时候,发现这么一篇介绍匈牙利算法的文章,非常通俗易懂,所以就借鉴过来了. 复杂度:邻接矩阵:O(v^3)邻接表:O(V*E) 附上链接:趣写算法系列之--匈牙利算法 下面就附上代码吧 ...

  10. javascript 图片延迟加载

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...