LeetCode 228. Summary Ranges

2022-03-10 16:58:55 浏览数 (1)

题目

代码语言:javascript复制
class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        
        vector<string> ans;
        
        if(nums.size()==0)
            return ans;
        
        int left=nums[0];
        int right;
        string str="";
        for(int i=1;i<nums.size();i  )
        {
            if(nums[i]!=nums[i-1] 1)
            {
                right=nums[i-1];
                str =to_string(left);
                if(left!=right)
                {
                    str ="->";
                    str =to_string(right);
                }
                
                left=nums[i];
                ans.push_back(str);
                str="";
            }
        }
        
        right=nums[nums.size()-1];
        str =to_string(left);
        if(left!=right)
        {
            str ="->";
            str =to_string(right);
        }
        ans.push_back(str);
        
        return ans;
        
    }
};

0 人点赞