LeetCode 228. Summary Ranges

原创 2016年05月16日 07:55:36
#include <vector>
#include <string>
#include <iostream>
using namespace std;

/*
  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"]
*/
vector<string> summaryRanges(vector<int>& nums) {
  int i = 0;
  vector<string> res;
  while(i < nums.size()) {
    int tmp = nums[i];
    int j = i + 1;
    while((j < nums.size()) && ((nums[j] - nums[i]) == (j - i))) {
      j++;
    }
    if(j - i >= 2) {
      string a = to_string(tmp) + "->" + to_string(nums[j-1]);
      res.push_back(a);
    } else {
      string a  = to_string(tmp);
      res.push_back(a);
    }
    i = j - 1;
    i++;
  }
  return res;
}

int main(void) {
  vector<int> nums{0, 1, 2, 3, 4, 5, 7};
  vector<string> res = summaryRanges(nums);
  for(int i = 0; i < res.size(); ++i) {
    cout << res[i] << " ";
  }
  cout << endl;
}


Second Round: Two pointers seem easier.

// summary ranges
#include <string>
#include <vector>
#include <iostream>
using namespace std;

vector<string> summaryRanges(vector<int>& nums) {
  vector<string> res;
  if(nums.size() == 0) return {};
  int start = nums[0], end = nums[0] + 1;
  for(int i = 1; i < nums.size(); ++i) {
    if(nums[i] == end) {
      end++;
    } else {
      if(end - start < 2) {
        string tmp = to_string(start);
        res.push_back(tmp);
      } else {
        string tmp = to_string(start) + "->" + to_string(end - 1);
        res.push_back(tmp);
      }
      start = nums[i];
      end = start + 1;
    }
  }
  if(end - start >= 2) {
    string tmp = to_string(start) + "->" + to_string(end - 1);
    res.push_back(tmp);
  } else res.push_back(to_string(start));
  return res;
}

int main(void) {
  vector<int> nums{0, 1, 2};
  vector<int> nums_1{1, 3, 5, 6};
  vector<string> res = summaryRanges(nums_1);
  for(string r : res) cout << r << endl;
}


版权声明:欢迎转载 https://blog.csdn.net/github_34333284/article/details/51422125

leetcode 228: Summary Ranges

leetcode 228: Summary Ranges python, java, c++
  • xudli
  • xudli
  • 2015-06-26 06:22:18
  • 10259

Leetcode_228_Summary Ranges

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

LeetCode - 228. Summary Ranges - 思路详解- C++

假设有一个已排序整数数组,不包含重复的元素,返回其范围摘要
  • SJWL2012
  • SJWL2012
  • 2017-01-13 20:50:21
  • 190

Summary Ranges - LeetCode 228

题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For examp...
  • bu_min
  • bu_min
  • 2015-06-28 09:11:16
  • 279

LeetCode228——Summary Ranges

一个月没写C++代码,现在感到好陌生 Given a sorted integer array without duplicates, return the summary of its range...
  • booirror
  • booirror
  • 2015-07-09 00:25:58
  • 1865

LeetCode OJ 系列之228 Summary Ranges --Python

Problem: Given a sorted integer array without duplicates, return the summary of its ranges. For ex...
  • ln152315
  • ln152315
  • 2015-11-16 09:11:30
  • 777

Summary Ranges(数组中连续的数字段)

Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. ...
  • u010032372
  • u010032372
  • 2015-06-26 11:53:18
  • 624

【LEETCODE】228-Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2015-12-16 16:24:15
  • 277

[LeetCode] Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges.For example, given...
  • u011331383
  • u011331383
  • 2015-06-30 20:04:26
  • 1346

<LeetCode OJ> 228. Summary Ranges

228. Summary Ranges My Submissions Question Total Accepted: 36554 Total Submissions: 159532 Diff...
  • EbowTang
  • EbowTang
  • 2016-02-10 16:11:10
  • 825
收藏助手
不良信息举报
您举报文章:LeetCode 228. Summary Ranges
举报原因:
原因补充:

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