zoukankan      html  css  js  c++  java
  • 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;
    
        }
    };
  • 相关阅读:
    android 75 新闻列表页面
    android 74 下载文本
    android 73 下载图片
    android 72 确定取消对话框,单选对话框,多选对话框
    android 71 ArrayAdapter和SimpleAdapter
    android 70 使用ListView把数据显示至屏幕
    maven如何将本地jar安装到本地仓库
    Centos6.7搭建ISCSI存储服务器
    解决maven打包编译出现File encoding has not been set问题
    MySQL 解决 emoji表情 的方法,使用utf8mb4 字符集(4字节 UTF-8 Unicode 编码)
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7245998.html
Copyright © 2011-2022 走看看