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. BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 439  Solved: 379[Submit][Status][Discuss] Descriptio ...

  2. 一次JVM内存调优过程

    项目中,有个同事写的JOB,使用到查询数据库大量历史协议数据(大概300W左右),由于对存放数据的list或map没有做“用完即时声明释放”. 导致此Jar部署在windows service后,进程 ...

  3. java日常规范

    1.方法返回类型用int还是Integer 看需求,int是数据类型,不能包含null等. Integer是类,其中包含该类中属性和方法,包含null. 所以建议使用Integer,方便以后拓展. 2 ...

  4. JS参考手册

    一.JavaScript Core API 词法结构 字符集 使用Unicode字符集 注释 单行注释 //或HTML风格的<!-- 多行注释 /**/ 标识符 大小写 区分大小写 空格.换行. ...

  5. git pull 发生冲突解决办法

    冲突原因:远程仓库的同一个文件的代码,和本地的文件代码不一样 解决办法 : 1.git stash (把本地冲突的代码隐藏) 2.git pull 3.git stash pop (将隐藏的和pull ...

  6. day 33 线程

    1.线程理论 什么是线程:线程是cpu的最小执行单位(实体),进程是操作系统的数据资源分配单位   2.线程的两种创建方式(重点) 查看线程的pid:使用os模块查看id,线程的id应该是相同的 3. ...

  7. TF-IDF介绍

    TF-IDF是什么 TF-IDF是一种统计方法,用以评估一个词对于一篇文章或语料库中一篇文章的重要性.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降. T ...

  8. Hadoop(二)CentOS7.5搭建Hadoop2.7.6完全分布式集群

    一 完全分布式集群(单点) Hadoop官方地址:http://hadoop.apache.org/ 1  准备3台客户机 1.1防火墙,静态IP,主机名 关闭防火墙,设置静态IP,主机名此处略,参考 ...

  9. hadoop 提交程序并监控运行

    程序编写及打包 使用maven导入第三方jar pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  10. 树莓派安装samba服务

    1.安装 sudo apt-get update sudo apt-get install samba sudo apt-get install samba-common-bin 2.配置 sudo ...