题目描述:
    给定一个无重复元素的有序整数数组 nums 。

    返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表。也就是说,nums 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 nums 的数字 x 。

    列表中的每个区间范围 [a,b] 应该按如下格式输出:

    “a->b” ,如果 a != b
    “a” ,如果 a == b

    链接:https://leetcode-cn.com/problems/summary-ranges

    思路:双指针,low记录开始下标,i找到递增临界点
    代码实现:

    1. class Solution {
    2. public List<String> summaryRanges(int[] nums) {
    3. int low = 0,fast = 0;
    4. int i = 0,n = nums.length;
    5. List<String> result = new ArrayList<String>();
    6. while(i < n) {
    7. low = i;
    8. i++;
    9. while (i < n && nums[i]- nums[i-1] == 1) {
    10. i++;
    11. }
    12. fast = i-1;
    13. StringBuffer res = new StringBuffer();
    14. res.append(nums[low]);
    15. if (low != fast) {
    16. res.append("->");
    17. res.append(nums[fast]);
    18. }
    19. result.add(res.toString());
    20. }
    21. return result;
    22. }
    23. }