LEETCODE--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”].
方法一:
本问题最大的障碍就是实现将int类型转换为字符串;
在此方法中使用了sprintf()函数
百度百科介绍sprintf
详细讲解sprintf()

我的理解:
我认为sprintf是C语言中的函数,因为在使用时括号中的参数需要定义数据类型(如本程序中的%d),而且只能用诸如char a[]的字符串数组进行操作(sprintf与printf的区别就是,sprintf是将结果输出在字符串中,而printf将结果输出在控制台窗口中);
另外:为了配合c++操作在Add程序的返回时进行强制类型转换(return string(buffer);)

class Solution {
public:
    string Add(int begin, int end){
        char buffer[30];
        if(end == begin)
            sprintf(buffer,"%d",end);
        else
            sprintf(buffer,"%d->%d",begin, end);
        return string(buffer);
    }
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> finsh;
        int len = nums.size();
        if(len == 0)
            return finsh;
        int end = nums[0];
        int begin = nums[0];
        for(int i = 1; i < len; i++){
            if(end == nums[i] || (end + 1) == nums[i])
                end = nums[i];
            else{
                string next =  Add(begin, end);
                finsh.push_back(next);
                begin = nums[i];
                end = nums[i];
            }
        }
        string next = Add(begin, end);
        finsh.push_back(next);
    }
};

方法二:
此方法使用了to_string()来进行类型转换;
详解to_string

class Solution {
public:
    string Add(int begin, int end){
        string s;
        if(begin == end)
            s = to_string(end);
        else{
            s = to_string(begin) + "->";
            s += to_string(end);
        }
        return s;
    }
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> finsh;
        int len = nums.size();
        if(len == 0)
            return finsh;
        int end = nums[0];
        int begin = nums[0];
        for(int i = 1; i < len; i++){
            if(end == nums[i] || (end + 1) == nums[i])
                end = nums[i];
            else{
                string next =  Add(begin, end);
                finsh.push_back(next);
                begin = nums[i];
                end = nums[i];
            }
        }
        string next = Add(begin, end);
        finsh.push_back(next);
    }
};

方法三:
利用stringstream流进行存储转换;
参考
注意:stringstream类型变量的位置不能改变,要优先写在每行的开头;

class Solution {
public:
    string Add(int begin, int end){
        string s;
        string s1;
        if(begin == end){
            stringstream ss; 
            ss << end;
            ss >> s1;
            s = s1;
        }
        else{
            stringstream ss; 
            ss << begin;
            ss >> s1;
            ss.clear();
            s += s1;
            s +='-';
            s +='>';
            ss << end;
            ss >> s1;
            s += s1;
        }
        return s;
    }
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> finsh;
        int len = nums.size();
        if(len == 0)
            return finsh;
        int end = nums[0];
        int begin = nums[0];
        for(int i = 1; i < len; i++){
            if(end == nums[i] || (end + 1) == nums[i])
                end = nums[i];
            else{
                string next =  Add(begin, end);
                finsh.push_back(next);
                begin = nums[i];
                end = nums[i];
            }
        }
        string next = Add(begin, end);
        finsh.push_back(next);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值