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