[LeetCode]Summary Ranges

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<String>();
        int length = nums.length;
        int left = 0;
        for (int i = 0; i < length; i++) {
            if (i + 1 == length || nums[i] + 1 != nums[i + 1]) {
                result.add(helper(nums, left, i));
                left = i + 1;
            }
        }
        return result;
    }
    public String helper(int[] nums, int left, int right) {
        if (left == right) {
            return String.valueOf(nums[left]);
        } else {
            return String.valueOf(nums[left]) + "->" + String.valueOf(nums[right]);
        }
    }
}

 

posted @ 2015-12-14 05:37  Weizheng_Love_Coding  阅读(117)  评论(0编辑  收藏  举报