# 1 题目描述

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"].

# 2 解题思路

1.通过+1判断是否为连续，如果连续则继续判断；如果不连续，则将前面连续的加入到List中。

2.最后将前一个添加进List中。

# 3 源代码

package com.larry.easy;

import java.util.ArrayList;
import java.util.List;

public class SummaryRanges {
public List<String> summaryRanges(int[] nums) {
List<String> ret = new ArrayList<String>();
int len = nums.length;
if(len == 0) ;
else {
int first = nums[0], secd = nums[0];
for(int i = 1; i < len; i++){
//与前一个相邻
if(nums[i] == nums[i-1] + 1) secd = nums[i];
else{
//处理字符串
else ret.add(first + "->" + secd);
//处理两个标记
first = nums[i];
secd = nums[i];
}
}
//最后需要处理一次
else ret.add(first + "->" + secd);
}
return ret;
}
public static void main(String[] args) {
int[] arr = {0,1,2,4,5,7};
int[] nums = arr;
System.out.println(new SummaryRanges().summaryRanges(nums).toString());
}
}


#### leetcode 228: Summary Ranges

2015-06-26 06:22:18

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

2017-01-13 20:50:21

#### Leetcode_228_Summary Ranges

2015-07-05 11:07:31

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

2015-06-26 11:53:18

#### LeetCode 228: Summary Ranges

2015-06-26 18:32:00

#### [Java]Summary Ranges 统计区间

2015-08-17 09:37:22

#### summary ranges

2015-06-30 03:56:51

#### Summary ranges

2015-09-16 07:42:07

#### Summary Ranges 总结区间

2016-08-10 18:20:49

#### Summary Ranges 228

2016-06-04 20:02:54

Summary Ranges