文章目录
题目
代码(12.13 首刷部分看解析)
String.format
可以做到快速的字符串转换。
当然还有int转string和string转int:
String.toString(int)
Integer.valueOf(String)
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
if(nums.length == 0)
return res;
int i = 0, j = 0;
int n = nums.length;
while(j < n) {
while(j + 1 < n && nums[j+1] == nums[j] + 1)
j++;
res.add(transform(nums, i, j));
// System.out.println(res.get(res.size()-1));
j++;
i = j;
}
return res;
}
public String transform(int[] nums, int l, int r) {
return l == r ? nums[l] + "" : String.format("%d->%d", nums[l], nums[r]);
}
}