2021-01-10 228.汇总区间

228.汇总区间

  • 用了Integer.toString把数字变成字符
    我们判断区间首尾是否相同,如果相同那就输出"数字",否则“数字->数字”。
class Solution {
    
    
    public List<String> summaryRanges(int[] nums) {
    
    
        

        int i = 0;
        List<String> res = new LinkedList<String>();

        while(i<nums.length){
    
    
            int start = nums[i];
            int j = 0;

            
            while(i<nums.length && nums[i]==(start+j)){
    
    
                i++;
                j++;  
            }

            if(j!=1)
                res.add(Integer.toString(start)+"->"+Integer.toString(nums[i-1]));
            else
                res.add(Integer.toString(start));
        }
        
        // System.out.println("+Integer.toString(-1))+"\"";

        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44495738/article/details/112424071