关闭

Leetcode——228. Summary Ranges

标签: leetcode
166人阅读 评论(0) 收藏 举报
分类:

题目

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

解答

这一题很简答,主要是把数字转化为字符串,可以使用itoa,也可以使用to_string,但是在leetcode上使用itoa,貌似没有包含这个,就自己写了一个。

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> res;
        const int size_n=nums.size();
        if(size_n==0) return res;
        for(int i=0;i<size_n;){
            int start=i,end=i;
            while(end+1<size_n&&nums[end]+1==nums[end+1]) 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+1;
        }
        return res;
    }
};
class Solution1 {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> res;
        if (nums.size() == 0) return res;
        int i = 0;
        string end_s = "";
        string res_temp;
        while (i<=nums.size() - 1)
        {
            int start = i;
            char p[20];
            itoa(nums[start], p, 10);
            res_temp = p;
            int end = i;
            while (i<nums.size() - 1&&nums[i + 1] - nums[i] == 1)
            {   
                i++;
                end = i;
            }
            i++;
            if(start!=end)
            {
                itoa(nums[end], p, 10);
                end_s = p;
                res_temp = res_temp + "->";
                res_temp = res_temp + end_s;
            }
            res.push_back(res_temp);
        }
        return res;
    }
private:

    void itoa(int num, char *str, int radix)//这里不考虑其他进制的
    {
        char index[] = "0123456789ABCDEF";
        unsigned unum;
        int i = 0, j, k;
        if (radix == 10 && num<0)//10进制负数
        {
            unum = (unsigned)-num;//unsigned是为了考虑 INT_MIN这种情况
            str[i++] = '-';
        }
        else
            unum = (unsigned)num;
        do {
            str[i++] = index[unum % (unsigned)radix];
            unum /= radix;
        } while (unum);
        str[i] = '\0';
        if (str[0] == '-') k = 1;
        else k = 0;
        char temp;
        for (j = k; j<=(i - 1)/ 2; j++)//反转
        {
            temp = str[j];
            str[j] = str[i - 1 + k - j];
            str[i - 1 + k - j] = temp;
        }
    }
};
0
0
查看评论

leetcode 228: Summary Ranges

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

LeetCode228——Summary Ranges

一个月没写C++代码,现在感到好陌生 Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return [&quo...
  • booirror
  • booirror
  • 2015-07-09 00:25
  • 1834

Leetcode_228_Summary Ranges

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted integer array without duplicates, return the summary...
  • pistolove
  • pistolove
  • 2015-07-05 11:07
  • 1046

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

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

228. Summary Ranges LeetCode

题意:给一个排好序的没有重复元素的数组,现在让你合并连续的部分。 题解:模拟即可。class Solution { public: vector<string> summaryRanges(vector<int>& nums) { int n...
  • youthinkwu
  • youthinkwu
  • 2016-02-12 13:44
  • 113

[leetcode] 228. Summary Ranges

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",&qu...
  • TstsUgeg
  • TstsUgeg
  • 2015-12-22 10:01
  • 213

LeetCode *** 228. Summary Ranges

题目: 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"...
  • treeshy
  • treeshy
  • 2016-04-07 21:42
  • 85

leetcode 228. Summary Ranges

leetcode 228. Summary Ranges
  • weililansehudiefei
  • weililansehudiefei
  • 2016-03-31 16:22
  • 156

[LeetCode] 228. Summary Ranges

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”].c...
  • sai_j
  • sai_j
  • 2017-07-22 11:36
  • 44

[leetcode]228. Summary Ranges

class Solution { public: vector summaryRanges(vector& nums) { int n=nums.size(); vector ret; string s=""...
  • lady_lili
  • lady_lili
  • 2016-08-29 22:09
  • 108
    个人资料
    • 访问:103999次
    • 积分:2273
    • 等级:
    • 排名:第19481名
    • 原创:150篇
    • 转载:22篇
    • 译文:0篇
    • 评论:27条
    最新评论