Summary Ranges

原创 2015年07月08日 09:23:06

解决问题的思路:两指针算法

代码如下:

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;
        string buf;
        vector<int>::iterator iter1, iter2;
        iter1 = nums.begin();
        iter2 = nums.begin();
        int counter;
        for(; iter1 < nums.end(); ) {
            buf.clear();
            counter = 0;
            for(iter2 = iter1 + 1; iter2 < nums.end(); ) {
                if((*iter2 - *(iter2 - 1) == 1) || (*iter2 - *(iter2 - 1) == 0)) {
                    iter2++;
                    counter++;
                }else
                    break;
            }
            buf += std::to_string(*iter1);
            if(counter > 0) {
                buf += "->";
                buf += std::to_string(*(iter2 - 1));
            }

            result.push_back(buf);

            iter1 = iter2;
        }
        return result;
    }
};

Leetcode_228_Summary Ranges

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted in...

leetcode--Summary Ranges

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

【LeetCode OJ 228】Summary Ranges

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

LeetCode228——Summary Ranges

leetcode刷题日记——Summary Ranges 1.题目内容: Given a sorted integer array without duplicates, return the su...

Leetcode——228. Summary Ranges

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

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

Summary Ranges 问题解答

前段时间刷了几道leetcode上面的题,觉得比较有意思,但由于没有记录,所以过一段时间就忘记了。计划在这里对每刷的一道题都进行总结,本次的题如下:题目 Given a sorted integer...

Summary Ranges

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

228. Summary Ranges\59. Spiral Matrix II\64. Minimum Path Sum

Summary Ranges 题目描述 代码实现 Spiral Matrix II 题目描述 代码实现 Minimum Path Sum 题目描述 代码实现228. Summary Ranges题目描...

Data Type Ranges

For 32-bit and 64-bit compilers, Microsoft Visual C++ recognizes the types shown in the table below....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Summary Ranges
举报原因:
原因补充:

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