LeetCode(228) Summary Ranges

原创 2015年11月18日 15:28:27

题目

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

分析

题目要求很明显,要求对给定的一组有序整数序列按照连续性分组显示;

一次遍历记录连续子序列的首尾元素,然后转换为string格式,整数和字符串格式类型转换需要特别注意INT_MIN时特殊处理;

AC代码

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        if (nums.empty())
            return vector<string>();

        int sz = nums.size();

        vector<string> ret;
        int start = nums[0], end = nums[0];
        for (int i = 1; i < sz; ++i)
        {
            if (nums[i] == end + 1)
                end = nums[i];
            else{
                string tmp;
                if (end == start)
                    tmp = IntToString(start);
                else
                    tmp = IntToString(start) + "->" + IntToString(end);

                ret.push_back(tmp);

                start = nums[i];
                end = nums[i];
            }
        }//for

        //加上最后一组
        string tmp;
        if (end == start)
            tmp = IntToString(start);
        else
            tmp = IntToString(start) + "->" + IntToString(end);
        ret.push_back(tmp);

        return ret;
    }

    string IntToString(long num)
    {
        if (num == 0)
            return "0";     
        else if (num == INT_MIN)
            return "-2147483648";       
        string str;
        bool flag = num < 0 ? false : true;

        num = abs(num);
        while (num)
        {
            char c = num % 10 + '0';
            num /= 10;
            str += c;
        }//while
        reverse(str.begin(), str.end());

        if (flag)
            return str;
        else
            return "-" + str;
    }
};

GitHub测试程序源码

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode_228_Summary Ranges

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

【LeetCode OJ 228】Summary Ranges

题目链接:https://leetcode.com/problems/summary-ranges/ 题目: Given a sorted integer array without dupl...

Leetcode——228. Summary Ranges

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

LeetCode228——Summary Ranges

leetcode刷题日记——Summary Ranges 1.题目内容: Given a sorted integer array without duplicates, return the su...

228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • iFuMI
  • iFuMI
  • 2016年03月22日 17:13
  • 209

228. Summary Ranges\59. Spiral Matrix II\64. Minimum Path Sum

Summary Ranges 题目描述 代码实现 Spiral Matrix II 题目描述 代码实现 Minimum Path Sum 题目描述 代码实现228. Summary Ranges题目描...

leetcode--Summary Ranges

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

Summary Ranges 问题解答

前段时间刷了几道leetcode上面的题,觉得比较有意思,但由于没有记录,所以过一段时间就忘记了。计划在这里对每刷的一道题都进行总结,本次的题如下:题目 Given a sorted integer...

Summary Ranges

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

Leetcode 163. Missing Ranges

163. Missing Ranges Total Accepted: 18160 Total Submissions: 60189 Difficulty: Medium Cont...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode(228) Summary Ranges
举报原因:
原因补充:

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