标签:
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"].
1 public class Solution { 2 public List<String> summaryRanges(int[] nums) { 3 List<String> res = new ArrayList<String>(); 4 if (nums==null || nums.length==0) return res; 5 int l=0, r=0; 6 for (; r<nums.length; r++) { 7 if (r<nums.length-1 && nums[r]!=nums[r+1]-1 || r==nums.length-1) { 8 StringBuffer temp = new StringBuffer(); 9 if (l == r) { 10 temp.append(nums[l]); 11 res.add(temp.toString()); 12 } 13 else { 14 temp.append(nums[l]); 15 temp.append("->"); 16 temp.append(nums[r]); 17 res.add(temp.toString()); 18 } 19 l = r + 1; 20 } 21 } 22 return res; 23 } 24 }
标签:
原文地址:http://www.cnblogs.com/EdwardLiu/p/5059022.html