Summary Ranges —— LeetCode

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> res = new ArrayList<>();
        if(nums==null||nums.length==0){
            return res;
        }
        for(int i=0;i<nums.length;i++){
            int t = nums[i];
            while(i<nums.length-1&&nums[i]+1==nums[i+1]){
                i++;
            }
            String se = "" + t;
            if(t!=nums[i])
                se = t+"->"+nums[i];
            res.add(se);
        }
        return res;
    }
}

 

posted @ 2015-06-29 22:41  丶Blank  阅读(201)  评论(0编辑  收藏  举报