LeetCode228——Summary Ranges

原创 2015年07月09日 00:25:58

一个月没写C++代码,现在感到好陌生

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

Credits: Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

vector<string> summaryRanges(vector<int>& nums) {
    vector<string> vs;
    if (nums.size() == 0) return vs; 
    int begin = nums[0];
    int end = nums[0];
    char temp[256];
    for (int i = 1; i < nums.size(); i++){
        if (nums[i] != end+1) {
            if (begin != end) {
                sprintf(temp, "%d->%d", begin, end);
            }else {
                sprintf(temp, "%d",end);
            }
            string s(temp);
            vs.push_back(s);
            begin = nums[i];
        }
        end = nums[i];
    }
    if (begin != end) {
        sprintf(temp, "%d->%d", begin, end);
    }else {
        sprintf(temp, "%d",end);
    }
    string s(temp);
    vs.push_back(s);
    return vs;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/booirror/article/details/46810977

LeetCode228:Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges.For example, given...
  • u012501459
  • u012501459
  • 2015-06-26 16:08:43
  • 624

leetcode 228: Summary Ranges

leetcode 228: Summary Ranges python, java, c++
  • xudli
  • xudli
  • 2015-06-26 06:22:18
  • 10259

LeetCode228——Summary Ranges

一个月没写C++代码,现在感到好陌生 Given a sorted integer array without duplicates, return the summary of its range...
  • booirror
  • booirror
  • 2015-07-09 00:25:58
  • 1865

<em>LeetCode</em>最全代码

# [<em>LeetCode</em>](https://<em>leetcode</em>.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [...
  • 2018年04月17日 00:00

LeetCode - 228. Summary Ranges - 思路详解- C++

假设有一个已排序整数数组,不包含重复的元素,返回其范围摘要
  • SJWL2012
  • SJWL2012
  • 2017-01-13 20:50:21
  • 190

LeetCode 228: Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, give...
  • sunao2002002
  • sunao2002002
  • 2015-06-26 18:32:00
  • 3878

Leetcode_228_Summary Ranges

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted in...
  • pistolove
  • pistolove
  • 2015-07-05 11:07:31
  • 1112

Leetcode 228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, g...
  • u012614906
  • u012614906
  • 2017-04-05 15:17:18
  • 962

LeetCode 228 Summary Ranges

题目描述Given a sorted integer array without duplicates, return the summary of its ranges.For example, g...
  • Yano_nankai
  • Yano_nankai
  • 2015-12-11 09:59:27
  • 503

(数组)leetcode 228: Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • Nitpicking
  • Nitpicking
  • 2016-01-05 21:47:58
  • 268
收藏助手
不良信息举报
您举报文章:LeetCode228——Summary Ranges
举报原因:
原因补充:

(最多只允许输入30个字)