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"].

My Solution:

vector<string> summaryRanges(vector<int>& nums)
{
vector<string> ret;
if(nums.empty())return ret;
int low=nums[];
bool flag=false;
for(int i=;i<nums.size();i++){
while(i<nums.size() && nums[i]==nums[i-]+)i++;
int high=nums[i-];
if(low!=high)
         ret.push_back(to_string(low)+"->"+to_string(high));
else
ret.push_back(to_string(low));
if(i==nums.size()){
flag=true;
break;
}
low=nums[i];
}
if(flag==false)ret.push_back(to_string(low));
return ret;
}

Better Solution:

 class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> vec;
if(nums.empty())
return vec; int low=nums[],high=nums[];
for(int i = ; i < nums.size(); i ++)
{
if(nums[i]-nums[i-] == )high=nums[i];
else
{
string range;
if(low != high)
range = to_string(low) + "->" + to_string(high);
else
range = to_string(low);
vec.push_back(range);
low = nums[i];
high = nums[i];
}
}
string range;
if(low != high)
range = to_string(low) + "->" + to_string(high);
else
range = to_string(low);
vec.push_back(range);
return vec;
}
};

【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

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

  4. 【LeetCode】163. Missing Ranges 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  5. leetcode面试准备:Summary Ranges

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

  6. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. 重新格式化hdfs系统的方法

    重新格式化hdfs系统的方法: (1)查看hdfs-ste.xml <span style="font-size:18px;"><property> < ...

  2. Windows API 进程状态信息函数

    这里的进程状态信息函数主要分为两类,一类是PS(PROCESS STATUS HELPER) API,另外一类是Th(TOOL HELP) API. 话说第一次遇到这个ToolHelp函数时我在看&l ...

  3. java遍历Map时remove删除元素

    public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...

  4. Map和hash_map

    map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...

  5. 标准模板库(STL)学习探究之stack

    标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string

  6. Win8环境下 IIS6部署MVC网站出现的无法显示此网页错误

    在Win7环境下做好的网站,新的Win8环境发布出现如下图错误: 解决方法如下: 运行:

  7. website project team member 角色及开发过程概念图

    一个web项目的团队往往具有以下角色的人员组成: project stakeholder(client or business owner)产品经理 Project manager 项目经理 prod ...

  8. Asp.Net验证码1

    验证码html调用 验证码:<input name="> <img src="CodeHandler.ashx" id="imgCode&qu ...

  9. You must SET PASSWORD before executing this statement解决

    [转载] MySql5.6操作时报错:You must SET PASSWORD before executing this statement解决 转载: http://blog.csdn.net/ ...

  10. 图形编程(数值微分DDA)

    #include <iostream> #include <time.h> #include <stdio.h> #include <stdlib.h> ...