228. Summary Ranges
挺简单的没什么要点,也没有什么边界情况
1 public List<String> summaryRanges(int[] nums) { 2 List<String> res = new ArrayList<String>(); 3 if(nums.length == 0) { 4 return res; 5 } 6 for(int i = 0; i < nums.length; i++) { 7 int start = i; 8 int end = i; 9 while(end < nums.length - 1 && nums[end+1] == nums[end] + 1) { 10 end++; 11 } 12 i = end; 13 String cur = String.valueOf(nums[start]); 14 if(start != end) { 15 cur += "->"; 16 cur += String.valueOf(nums[end]); 17 } 18 res.add(cur); 19 } 20 return res; 21 }