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. first-child伪类选择器

    原文链接地址:https://www.cnblogs.com/wangmeijian/p/4562304.html :first-child 选择器用于选取属于其父元素的首个子元素的指定选择器.——w ...

  2. iOS之oc与html之间的交互(oc中调用js的方法)

    一.运行的效果图 1.刚开始的效果   2.运行结束后的效果   二.准备工作 1.准备一个html文件导入到oc工程中 2.jiaohu.html文件的原始内容   3.从oc语言中操作.html文 ...

  3. jQuery添加标签实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Java并发编程(九)线程间协作(下)

    上篇我们讲了使用wait()和notify()使线程间实现合作,这种方式很直接也很灵活,但是使用之前需要获取对象的锁,notify()调用的次数如果小于等待线程的数量就会导致有的线程会一直等待下去.这 ...

  5. js 时间转换毫秒的四种方法(转)

    将时间转换为毫秒数的方法有四个: Date.parse()Date.UTCvalueOf()getTime() 1. Date.parse():该方法接受一个表示日期的字符串参数,然后尝试根据这个日期 ...

  6. 替代alert的消息框和提示框

    alert提示框由于外观不太友好,所以一般都不用alert了. 我在这里使用bootstrap的样式,写了一个可以单独显示消息,也可以确认取消的提示框,确认,取消的采用模式对话框方式,用一个div遮盖 ...

  7. MySQL架构与引擎初识

    一.MySQL逻辑架构 1.连接层: 最上层是一些客户端和连接服务,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等. 2.服 ...

  8. js节点操作实例

    写了一个简单的小例子来引用js实例 1. 初步节点操作: 2.兼容性节点操作: 3.节点的类型,名字: 4.使用setAttribute设置属性 5.节点复制操作: 6.删除和替换节点 如有错误,还望 ...

  9. Flink-on-yarn

    介绍 官网下载 https://www.apache.org/dyn/closer.lua/flink/flink-1.6.1/flink-1.6.1-bin-hadoop28-scala_2.11. ...

  10. 准确率(accuracy),精确率(Precision),召回率(Recall)和综合评价指标(F1-Measure )----转

    原文:http://blog.csdn.net/t710smgtwoshima/article/details/8215037   Recall(召回率);Precision(准确率);F1-Meat ...