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

[LeetCode]Summary Ranges

时间:2015-12-14 06:49:47      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

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]);
        }
    }
}

 

[LeetCode]Summary Ranges

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5044044.html

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