[leetcode]228. Summary Ranges

原创 2016年08月29日 22:09:40
class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) 
    {
        int n=nums.size();
        vector<string> ret;
        string s="";
        
        int i=0;int j=0;
        while(i<n&&j<n)
        {
                while(j<n-1 && nums[j+1]-nums[j]==1)
                {
                    j++;
                }
                if(j+1==n-1)//考虑最后一个数的情形
                {
                    if(nums[j+1]-nums[j]==1)
                        j+=1;
                }
                
                if(j>i)
                { 
                    s=ToString(nums[i])+"->"+ToString(nums[j]); 
                }
                else //j==i
                {
                    s=ToString(nums[j]);
                }
                ret.push_back(s);
                i=j+1;
                j=i;
       
        }
        return ret;
    }
    string ToString(long long int n)//类型转换函数
    {
        string str;
        if(n<0)//考虑负数的情形
        {
            str.push_back('-');
            n=-n;
        }
        string str1;
        while(1)
        {
            int b=n%10;
            str1.push_back(b+48);//'0'+?
            n=n/10;
            if(n==0)
                break;
        }
        while(!str1.empty())
        {
            str.push_back(str1.back());//逆置
            str1.pop_back();
        }
        return str;
    }
};

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

相关文章推荐

Summary Ranges - LeetCode 228

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

Leetcode——228. Summary Ranges

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

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

<LeetCode OJ> 228. Summary Ranges

228. Summary Ranges My Submissions Question Total Accepted: 36554 Total Submissions: 159532 Diff...

【LeetCode OJ 228】Summary Ranges

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

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
  • 138

Leetcode 第228题 Summary Ranges

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

228. Summary Ranges

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

[LeetCode] Summary Ranges

Summary Ranges Given a sorted integer array without duplicates, return the summary of its range...

leetcode--Summary Ranges

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

Leetcode(3)-Summary Ranges

228 Summary Ranges For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].class Solution { pu...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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