Easy to understand JAVA


  • 0
    Y
    public class Solution {
        public List<String> summaryRanges(int[] nums) {
            List<String> list=new ArrayList();
            if (nums.length==0) return list;
            if (nums.length==1){
                list.add(nums[0]+"");
                return list;
            }
            int m=0;
            int n=0;
            for (int i=1;i<nums.length;i++){
                if ((nums[i]-nums[i-1])!=1){
                    n=i-1;
                    if (n-m==0) list.add(nums[n]+"");
                    else list.add(nums[m]+"->"+nums[n]);
                    m=i;
                }
                else n=i;
            }
            if (nums.length-1-m==0) list.add(nums[nums.length-1]+"");
            else list.add(nums[m]+"->"+nums[n]);
            return list;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.