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

class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
auto start=nums.begin(); while(start != nums.end())
{
auto end=start;
end++;
string tem="";
int x=*start;
itos(tem,x);
while(end != nums.end() && *end - *start == 1){
start++;
end++;
}
if(x == *start)
res.push_back(tem);
else
{
tem+="->";
itos(tem,*start);
res.push_back(tem);
}
start++;
}
return res;
}
void itos(string &str,int k) //主要是如何快速转换成字符串!!!
{
ostringstream oss;//创建一个流 oss<<k;//把值传递如流中 str+=oss.str();
}
};

  

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. C#解leetcode 228. Summary Ranges Easy

    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. [leetcode]228. Summary Ranges区间统计

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

  8. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  9. leetcode-【中等题】228. Summary Ranges

    题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...

随机推荐

  1. POJ 1700 F(Contest #3)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

  2. Android listview 制作表格样式+由下往上动画弹出效果实现

    效果是这样的:点击按下弹出表格的按钮,会由下往上弹出右边的列表,按下返回按钮就由上往下退出界面. 布局文件: activity_main.xml <RelativeLayout xmlns:an ...

  3. 事件函数SetEvent、PulseEvent与WaitForSingleObject详解

    系统核心对象中的Event事件对象,在进程.线程间同步的时候是比较常用,发现它有两个出发函数,一个是SetEvent,还有一个PulseEvent, 两者的区别是: SetEvent为设置事件对象为有 ...

  4. 完美替换win8.1 x64默认雅黑为宋体方法

    由于显示器不好,安装win8.1 x64系统后,系统默认的雅黑字体实在是看不习惯, 时间稍一常眼睛就发累,还是一直用的xp宋体好,于是寻思着能不能将默认雅黑换成宋体. 网上倒是搜出了许多win7的修改 ...

  5. javascript面向对象知识点

    首先,声明何为对象:对象是键值对的集合 其次,声明:变量就是键值对 再次,声明:函数也是变量 1. JavaScript包含:ECMAScript(核心).DOM(文档对象模型)和BOM(浏览器对象模 ...

  6. treap模版代码

    treap模版暂存. 以后修改整理. #include<cstdio> #include<iostream> #include <time.h> #include& ...

  7. dllimport路径问题

    今天做了个试验,是针对dllimport("XXX.DLL");这样写的时候,系统是如何寻找该dll的. 首先系统会搜寻主应用程序根目录. 其次搜寻操作系统安装目录,一般情况是C: ...

  8. Tomcat容器运行struts2+spring+mybatis架构的java web应用程序简单分析

    1.具体的环境为 MyEclipse 8.5以及自带的tomcat spring3.0.5 struts2.3.15.1 mybatis3.0.5 2.想弄明白的一些问题 tomcat集成spring ...

  9. copy和assign的使用和区别

    1.使用copy和assign都可以进行修饰属性或者变量. 2.区别: (1)copy的使用:使用这个进行修饰的属性,当已经进行初始化之后,就无法再改变属性的数据. 如: @property (cop ...

  10. 用Qt图形视图框架开发拼图游戏

    用Qt的图形视图框架(Graphics View Framework)做了一个拼图游戏DEMO,演示了: QGraphicsView.QGraphicsScene.QGraphicsItem的基本用法 ...