Java easy to understand


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

Log in to reply
 

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