Leetcode 228. 汇总区间

题目

在这里插入图片描述
Leetcode 228. 汇总区间

代码(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]);
    }
}

相关推荐

  1. [leetcode] 228. 汇总区间

    2023-12-15 01:16:01       18 阅读
  2. 汇总区间算法(leetcode228题)

    2023-12-15 01:16:01       33 阅读
  3. 【数组】Leetcode 228. 汇总区间【简单】

    2023-12-15 01:16:01       11 阅读
  4. 【C++】每日一题 228 汇总区间

    2023-12-15 01:16:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 01:16:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 01:16:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 01:16:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 01:16:01       20 阅读

热门阅读

  1. Dockerfile模板和Docker Compose模板

    2023-12-15 01:16:01       41 阅读
  2. 基于sfunction builder的c-sfunction编写及案例测试分析

    2023-12-15 01:16:01       31 阅读
  3. Linux如何对文件进行分割和重组

    2023-12-15 01:16:01       43 阅读
  4. Py-While循环语句

    2023-12-15 01:16:01       33 阅读