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

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.



class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
const int size_n = nums.size();
vector<string> res;
if ( == size_n) return res;
for (int i = ; i < size_n;) {
int start = i, end = i;
while (end + < size_n && nums[end+] == nums[end] + ) end++;
if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else res.push_back(to_string(nums[start]));
i = end+;
}
return res;
}
};
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> result;
if(!nums.size()) return result;
int low = nums[], high = nums[];
for(int i = ; i < nums.size(); i++){
if (nums[i] == high + )
high++;
else{
result.push_back(low == high ? to_string(low) : to_string(low) + "->" + to_string(high));
low = high = nums[i];
}
}
result.push_back(low == high ? to_string(low) : to_string(low) + "->" + to_string(high));
return result;
}
};

228. [LeetCode] Summary Ranges的更多相关文章

  1. LeetCode(228) Summary Ranges

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

  2. [LeetCode] Summary Ranges 总结区间

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

  3. (leetcode)Summary Ranges

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

  4. LeetCode——Summary Ranges

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

  5. LeetCode Summary Ranges (统计有序数列范围)

    题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...

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

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

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

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

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

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

  9. leetcode面试准备:Summary Ranges

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

随机推荐

  1. 并发编程(二)------并发类容器ConcurrentMap

    并发类容器: jdk5.0以后提供了多种并发类容器来替代同步类容器从而改善性能. 同步类容器的状态都是串行化的. 他们虽然实现了线程安全,但是严重降低了并发性,在多线程环境时,严重降低了应用程序的吞吐 ...

  2. code#5 P1 报告

    报告   时间限制: 1.0 秒 空间限制: 128 MB 相关文件: 题目目录 题目描述 企鹅高中有很多学生,自然管理起来也就非常麻烦.学校的教务处想要随时统计学校里面有多少个学生,但是他们只有很多 ...

  3. react脚手架环境搭建流程

    1.安装与配置node.js:1.1软件下载地址:https://nodejs.org/en/,推荐下载.msi文件,其中npm已经集成在了node.js中.1.2 双击下载的.msi文件进行安装,安 ...

  4. PHP目前比较常见的五大运行模式

    做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...

  5. 关于Xshell无法连接本地虚拟机的问题

    近期想搭建一个测试用的集群,但是!  刚开始搭第一台虚拟机就出现问题了,Xshell无法连接到虚拟机! 然后我更改了/etc/sysconfig/network-scripts/ifcfg-ens33 ...

  6. micro:bit 软件生态系统介绍

    microbit 软件分成在microbit (Target Computer 如下图右边)上执行的及主计算机(Host Computer 如下图左边)上两类 : 一般程序写好后透过USB 转到mic ...

  7. Go语言 异常panic和恢复recover用法

    Go语言 异常panic和恢复recover用法 背景:Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在 ...

  8. HTTP1.0,HTTP1.1,HTTP2.0的主要特征对比

    HTTP1.0 是一种无状态.无连接的应用层协议. HTTP1.0规定浏览器和服务器保持短暂的连接,浏览器的每次请求都需要与服务器建立一个TCP连接,服务器处理完成后立即断开TCP连接(无连接),服务 ...

  9. 20155202 2016-2017-2《Java程序设计》课程总结

    20155202 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:第一次写随笔,我眼中的师生关系--未来我的大学生活 预备作业2:第二次随笔-- ...

  10. 20155222 卢梓杰 myod

    20155222 卢梓杰 myod 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写Makefi ...