题目:

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. Android——黑名单

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. maven加载jar包配置

    maven build时报程序包不存在和找不到符号的错误,但是代码中不报错,如下: [ERROR] Failed to execute goal org.apache.maven.plugins:ma ...

  3. Cocos2d-x 3.x 错误 cocos2dxDownloader 编译报错

    因为自带的库不是自己写的代码,所以报错的当时就傻了 报错文件为Cocos2dxDownloader.java 错误铺满全屏,仔细查看发现是类的调用有问题,翻到最上边发现是有几个(很多个)类没有定义,我 ...

  4. js取整函数

    1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.fl ...

  5. 注册noip.com动态域名的步骤

    1.先打开这个网址 http://www.noip.com/ 2.点击页面右上绿色的Sing Up,进行注册 3.会出现这个页面,把这些填上 Create my hostname later这一项不打 ...

  6. maven 使用国内代理

    一直使用 maven默认仓库,maven 的下载速度一直不快不慢:最近换了网络,maven秒变蜗牛,换了阿里云的镜像,瞬间飞快. 配置文件 ${USER_HOME}/.m2/settings.xml: ...

  7. Java--super关键字用法

    看了马老师的视频,百度了一下,随即敲了一些代码,super是超人,超级的意思,层面上理解为父类 class Person { Person(){             System.out.prin ...

  8. [Spring MVC] - 从数据库读取MessageSource

    Spring MVC中使用MessageSource默认是写在properties文件当中,以支持国际化. 但很多时候我们需要把数据写到数据库当中,而不是在properties文件当中,以方便日常维护 ...

  9. 在eclipse运行程序

    1.开始的时候由于另一个财务共享的项目,其中也应用了maven管理项目,因此就不用额外安装配置maven. 2.这里关键的一点就是需要通过maven和jetty配合运行lenmonOA. 3.jett ...

  10. webstorm(注册,激活,破解,码,一起支持正版,最新可用)(2016.9.2更新)

    webstorm(注册,激活,破解,码,一起支持正版,最新可用)(2016.9.2更新)   来源于:http://blog.csdn.net/xx1710/article/details/51725 ...