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

Credits:

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



最开始是这么想的,可以用一个队列来存放连续的数字,这样在遍历数组的过程中将数依次放入队列中,当某一个数和前一个数不同时将队列中的数打印输出(如果队列中只有1个元素,将这个元素打印输出,如果队列中有多个元素,那么将队列的头和尾打印出来),然后将队列清空,并将这个和前面元素不同的数放入队列中。这样遍历到数组尾端时队列中一定还会有元素,再将元素打印出来就可以了。

runtime:0ms

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        int length=nums.size();
        vector<string> result;
        if(length<=0)
            return result;
        vector<int> bucket;
        bucket.push_back(nums[0]);
        for(int i=1;i<length;i++)
        {
            if(nums[i-1]==nums[i]-1)
            {
                bucket.push_back(nums[i]);
            }
            else
            {
                if(bucket.size()==1)
                    result.push_back(to_string(bucket[0]));
                else
                {
                    string str=to_string(bucket.front())+"->"+to_string(bucket.back());
                    result.push_back(str);
                }

                bucket.clear();
                bucket.push_back(nums[i]);
            }
        }
        if(!bucket.empty())
        {
                if(bucket.size()==1)
                    result.push_back(to_string(bucket[0]));
                else
                {
                    string str=to_string(bucket.front())+"->"+to_string(bucket.back());
                    result.push_back(str);
                }
        }
        return result;
    }
};


然后看了下Discuss,发现了一种更加优化的一种解法,没有上面太多的分支判断。就是从头开始遍历数组,一直找到第一个不连续的数,然后打印,接着再开始遍历。这样可以避免上面边界条件的处理。

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
       int length=nums.size();
       vector<string> result;
       if(length==0)
            return result;
        if(length==1)
        {
            result.push_back(to_string(nums[0]));
            return result;
        }
        for(int i=0;i<length;)
        {
            int start=i;
            int end=i;
            while(end+1<length&&nums[end]==nums[end+1]-1)
                end++;

            if(start<end)
            {
                string str=to_string(nums[start])+"->"+to_string(nums[end]);
                result.push_back(str);
            }
            else
                result.push_back(to_string(nums[start]));

            i=end+1;
        }
        return result;

    }
};
时间: 2024-10-18 03:49:55

LeetCode228:Summary Ranges的相关文章

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 ["0->2","4->5","7"]. Credits: Special thanks to @jianchao.li.fighter for add

[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"]. 代码 /*--------------------------------------- * 日期:2015-08-04 * 作者:SJF01

LeetCode:Summary Ranges - 获取数组中数字的范围

1.题目名称 Summary Ranges(获取数组中数字的范围) 2.题目地址 https://leetcode.com/problems/summary-ranges/ 3.题目内容 英文:Given a sorted integer array without duplicates, return the summary of its ranges. 中文:给出一个整数数组,返回这个数组中数字的范围 例如:给出数组 [0,1,2,4,5,7],返回 ["0->2",&quo

leetcode_228题——Summary Ranges

Summary Ranges Total Accepted: 9278 Total Submissions: 48419My Submissions Question Solution 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&quo

[LeetCode][JavaScript]Summary Ranges

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"]. https://leetcode.com/problems/summary-ranges/ 简单的模拟题,合并连续的数

[Swift]LeetCode228. 汇总区间 | Summary Ranges

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. Exa

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"]. 这个题目不难,首先对原序列进行排序,然后要注意对特殊情况的处理.同时,要掌握string类的几个重要成员函数: to_string():将数字转换为

Java for 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"]. 解题思路: JAVA实现如下: public List<String> summaryRanges(int[] nums) { List

Summary Ranges —— LeetCode

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"]. 题目大意:给一个有序数组,返回连续的区间范围. public class Solution { public List<String>