码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Missing Ranges

时间:2015-03-21 08:36:43      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

 1 class Solution {
 2 public:
 3     string getRange(int start, int end) {
 4         ostringstream oss;
 5         if (start == end) {
 6             oss << start;
 7         } else {
 8             oss << start << "->" << end;
 9         }
10         return oss.str();
11     }
12     vector<string> findMissingRanges(int A[], int n, int lower, int upper) {
13         vector<string> result;
14         int prev = lower - 1;
15         for (int i = 0; i < n; i++) {
16             if (A[i] - prev > 1) {
17                 result.push_back(getRange(prev+1, A[i]-1));
18             }
19             prev = A[i];
20         }
21         if (upper - prev > 0) result.push_back(getRange(prev+1, upper));
22         return result;
23         
24     }
25 };

 

LeetCode – Refresh – Missing Ranges

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4355116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!