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) {
int n=nums.size();
int i;
int left,right;
char str[];
left=;
right=;
vector<string> res;
if(n<)
return res;
for(i=;i<n;i++)
{
if(i==)
{
left=nums[i];
right=left;
}
else if(right+==nums[i])
{
right++;
}
else
{
if(left!=right)
{
sprintf(str,"%d->%d",left,right);
string tmp(str);
res.push_back(tmp);
}
else
{
sprintf(str,"%d",left);
string tmp(str);
res.push_back(tmp);
}
left=nums[i];
right=left; }
}
if(left!=right)
{
sprintf(str,"%d->%d",left,right);
string tmp(str);
res.push_back(tmp);
}
else
{
sprintf(str,"%d",left);
string tmp(str);
res.push_back(tmp);
}
return res;
}
};

Summary Ranges的更多相关文章

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

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

  2. Missing Ranges & Summary Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  3. leetcode面试准备:Summary Ranges

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

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

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

  5. 【刷题-LeetCode】228. Summary Ranges

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

  6. [LeetCode] Summary Ranges 总结区间

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

  7. Java for LeetCode 228 Summary Ranges

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

  8. (leetcode)Summary Ranges

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

  9. LeetCode(228) Summary Ranges

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

随机推荐

  1. sudo gem install cocoapods 没反应问题

    1. 尝试更新 sudo gem update --system 2. 查看安装详细 sudo gem install cocoapods -V 3.详细使用有个链接 http://blog.csdn ...

  2. 【读书笔记】iOS网络-HTTP-请求内容

    一,GET方法. 从服务器获取一段内容,用HTTP术语来说就是实体.GET请求通常不包含请求体,不过也是可以包含的.有些网络缓存设施只会缓存GET响应.GET请求通常不会导致服务器端的数据变化. 二, ...

  3. 【读书笔记】iOS网络-运行循环

    运行循环是由类NSRunLoop表示的,有些线程可以让操作系统唤醒睡眠的线程以管理到来的事件,而运行循环则是这些线程的基本组件.运行循环是这样一种循环,可以在一个周期内调度任务并处理到来的事件.iOS ...

  4. iOS 学习 - 8 TableViewCell 自适应高度

    思路:计算文字的高度,存进数组 加注:存在中文,需要加一行文字的高度,也就是 font 主要代码 #pragma mark -- UITableViewDelegate - (CGFloat)tabl ...

  5. eclipse 中手动安装 subversive SVN

    为什么我选择手动安装呢?因为通过 eclipse market 下载实在太慢了.   1.下载离线安装包 http://www.eclipse.org/subversive/latest-releas ...

  6. 菜鸟程序员之Asp.net MVC Session过期异常的处理

    小赵是刚毕业的计算机专业方面的大学生,4年的大学时间里面,他读过了很多编程方面的数据,也动手也了很多代码.现在毕业了,他如愿的加入了T公司,开始了自己的程序员生涯.他信心满满,相信自己4年的学习到的东 ...

  7. PS网页设计教程XXV——使用Photoshop设计的老式组合布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  8. 用WPF做了几个小游戏

    最近看书看累了,参考别人的代码(其实差不多就是把代码重新打了一遍o(╯□╰)o),用wpf做了个<2048>小游戏,顺便在<Git教程>学习下git,也顺便把在<写让别人 ...

  9. TCP/IP详解--TCP首部选项中时间戳选项

    一.简介 TCP时间戳选项会在TCP包头增加12个字节,以一种比重发超时更精确的方法来启用对RTT 的计算.   二.作用 ) TCP时间戳位于TCP选项中,kind=:lenth=:data由tim ...

  10. 图片代替radio

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...