题目:

228. Summary Ranges

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

答案:

就是找连续的序列。

直接判断相邻数据大小是否相差为1即可,主要是注意最后的数字要特殊考虑一下。

链接:

https://leetcode.com/problems/summary-ranges/

代码:

 #include <vector>
#include <string>
#include <sstream> using std::vector;
using std::string;
using std::ostringstream; class Solution {
private:
string num2str(int& num)
{
ostringstream stream;
stream << num;
return stream.str();
} public:
vector<string> summaryRanges(vector<int>& nums) {
unsigned int len = nums.size();
unsigned int index,inIndex;
unsigned int minIndex = ;
unsigned int maxIndex = ; vector<string> ans;
if(len == )
{
return ans;
} if(len == )
{
ans.push_back(num2str(nums[]));
return ans;
} for(index = ; index < len - ; ++ index)
{
if(nums[index] + == nums[index + ])
{
maxIndex = index + ;
}else
{
string tmp = "";
tmp += num2str(nums[minIndex]);
if(maxIndex > minIndex)
{
tmp += "->";
tmp += num2str(nums[maxIndex]);
}
ans.push_back(tmp); minIndex = index + ;
maxIndex = index + ;
}
} if(nums[len-] + == nums[len - ])
{
maxIndex = len - ;
}else
{
minIndex = len - ;
maxIndex = len - ;
} string tmp = "";
tmp += num2str(nums[minIndex]);
if(maxIndex > minIndex)
{
tmp += "->";
tmp += num2str(nums[maxIndex]);
}
ans.push_back(tmp); return ans;
}
};

leetcode-【中等题】228. Summary Ranges的更多相关文章

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

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

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

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

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

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

  4. C#解leetcode 228. Summary Ranges Easy

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

  5. [LeetCode] 228. Summary Ranges 总结区间

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

  6. Java for LeetCode 228 Summary Ranges

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

  7. LeetCode(228) Summary Ranges

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

  8. (easy)LeetCode 228.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. 《C与指针》第十五章练习

    本章例程 15.1打开和关闭文件 #include <stdlib.h> #include <stdio.h> int main(int ac, char **av) { in ...

  2. xloader

    [1],先看一下整个系统的结构(软件是灵魂,硬件是驱体,再强大的灵魂力若没有躯体终将是游魂野鬼,再强壮的驱体若没有灵魂终将是植物人) 结构 作用 备注 硬件 一切软件的载体   xloader 引导u ...

  3. 用c#中的WebBrowser抢小米F码,抢小米手机以及自动测试实现原理

    首先是用c#中的WebBrowser控件打开登录网页,很简单,拖拽WebBrowser到Form上,然后给它的Url属性赋值.WebBrowser就会自动navigate to 这个网页. WebBr ...

  4. 第一个Struts2程序之HelloWorld

    1.Struts2 简介 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与St ...

  5. 【亚瑟士 ASICS 系列】

    [新配色 36-44] [亚瑟士 黑薄荷 大工厂流线 36-44] [亚瑟士 阿斯克斯 星空 水洗丹宁 36-44] [亚瑟士 阿斯克斯 经典爆 鼠尾草 36-44] [亚瑟士 ASICS Gel S ...

  6. python-模块安装方法

    python程序会使用很多模块,通常安装模块的方法是找到官方网站,选择合适的版本下载安装,很麻烦.为了简化安装第三方的模块,python提供了easy_install工具,只需要一条命令就可以安装合适 ...

  7. javascript 利用匿名函数对象给你异步回调方法传参数

    先来创建一个匿名函数对象: /*** * 匿名函数 */ var callChangeBtn=new function(bugBtn){ this.chage=function(json){ bugB ...

  8. 用Sublime 3作为React Native的开发IDE- 转

    转-http://www.cnblogs.com/wangshuo1/p/react_native_02.html Sublime Text是一个代码编辑器.也是HTML和散文先进的文本编辑器.漂亮的 ...

  9. linux 维护常见场景小命令 (未完待续)

    1.安装KDE桌面 [root@rhel06 ~]# yum -y groupinstall "X Windows System" "KDE Desktop" ...

  10. 批处理定时重启print打印服务,解决打印机异常队列堆积

    公司有台打印机,由于是公用的,经常出现一个较大的文档卡在队列里面,导致队列后面的打印无法被执行,人工去清理岂不是太费事了,下面分享一个批处理文件 @echo off echo 计划任务开始执行 3 e ...