您的位置:首页 > 其它

【LeetCode】228 Summary Ranges

2015-12-01 17:39 204 查看
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”].

public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<String>();
for(int i = 0; i < nums.length; i++){
int start = nums[i];
if(i < nums.length - 1){
while(nums[i+1] == (nums[i]+1)){
i++;
if(i == nums.length - 1){
break;
}
}
}
int end = nums[i];
if(end != start){
list.add(start + "->" + end);
}else{
list.add(end+"");
}
}
return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode