228. Summary Ranges

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"].

 此题方法是,遍历一遍数组元素,设定当前数组元素为a,当cur的后面的元素-cur的元素==1的时候,i++操作,代码如下:

public class Solution {

    public List<String> summaryRanges(int[] nums) {

        List<String> res=  new ArrayList<String>();

        for(int i=0;i<nums.length;i++){

            int a = nums[i];

            while(i+1<nums.length&&nums[i+1]==nums[i]+1){

                i++;

            }

            if(a==nums[i]) res.add(nums[i]+"");

            else res.add(a+"->"+nums[i]);

        }

        return res;

    }

}

原文地址:https://www.cnblogs.com/codeskiller/p/6361071.html