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(); for (int i = 0; i < nums.length; i++){ int start = nums[i]; while (i < nums.length - 1 && nums[i + 1] - nums[i] == 1){ i++; } if (start != nums[i]){ list.add(new String(start + "->" + nums[i])); } else { list.add(String.valueOf(start)); } } return list; } }