题目:

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. AngularJS + Java---前台网页与后台数据库传递数据 基本结构

    第一个关于这两种语言的项目,以下只是我自己的理解,欢迎指教:) 基本对应关系 1. controller .jsp(.html)  ng-controller="controllerTest ...

  2. 浏览器与web客户端的HTTP交互过程

    未经许可谢绝以任何形式对本文内容进行转载! HTTP协议是常见的几种应用层协议之一,当我们用浏览器和web客户端进行交互时html页面等内容的传输都是依靠该协议完成的.值得注意的是,HTTP使用的是T ...

  3. android 获取SD卡相关信息

    Object localOb; String str1 = null; try { localOb = new FileReader("/sys/block/mmcblk0/device/t ...

  4. AWS CloudFormation Template

    { "AWSTemplateFormatVersion" : "2010-09-09", "Parameters" : { "Ba ...

  5. Hello World 程序

    1.windows 在d:/python35_study目录下创建hello.py文件,内容如下: print ('Hello World') 在cmd下执行进入d:/python35_study目录 ...

  6. ios打包出来为pkg的处理方法

    Add LSRequiresIPhoneOS YES to your Info.plist The key can be found as Application requires iPhone en ...

  7. MySql自增长列

    1. 关键字 auto_increment 2. 自增用法 a) create table test(tid int auto_increment, tname varchar(10), primar ...

  8. ActiveX控件打包、签名、嵌入详解

    ActiveX控件打包.签名.嵌入详解 前言 在我们的一个项目中,使用到了大华网络监控摄像头枪机,网络上下载了其ActiveX插件,但是发现其所提供的类库没有打包处理.这就导致我们每次给用户安装的时候 ...

  9. 【javascript 面试笔试】1、几道笔试题

    今天想起来几道javascript的面试题,大家做做看看,有别的思路可以在下面写出来,大家交流一下 (1) 将多维数组转化成一个一位数组,例如[1,[2,3],[4,5,[6,7]]]转化成[1,2, ...

  10. Dimmer: 通过移动鼠标来改变 LED 的亮度

    原文地址 - https://www.arduino.cc/en/Tutorial/Dimmer 调光器 本例展示了如何通过个人电脑发送数据到 Arduino / Genuino 开发板来控制一个LE ...