classSolution { public: vector<string> summaryRanges(vector<int>& nums){ vector<string> ret; int i = 0; int n = nums.size(); while (i < n) { int low = i; i++; while (i < n && nums[i] == nums[i - 1] + 1) { i++; } int high = i - 1; string temp = to_string(nums[low]); if (low < high) { temp.append("->"); temp.append(to_string(nums[high])); } ret.push_back(move(temp)); } return ret; } };
classSolution { public List<String> summaryRanges(int[] nums) { List<String> ret = newArrayList<String>(); inti=0; intn= nums.length; while (i < n) { intlow= i; i++; while (i < n && nums[i] == nums[i - 1] + 1) { i++; } inthigh= i - 1; StringBuffertemp=newStringBuffer(Integer.toString(nums[low])); if (low < high) { temp.append("->"); temp.append(Integer.toString(nums[high])); } ret.add(temp.toString()); } return ret; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcsummaryRanges(nums []int) (ans []string) { for i, n := 0, len(nums); i < n; { left := i for i++; i < n && nums[i-1]+1 == nums[i]; i++ { } s := strconv.Itoa(nums[left]) if left < i-1 { s += "->" + strconv.Itoa(nums[i-1]) } ans = append(ans, s) } return }