My Java Solution (224ms)


  • 0
    B
    public class Solution {
        public List<String> summaryRanges(int[] nums) {
            StringBuffer sb = new StringBuffer();
            List<String> list = new ArrayList<String>();
            if(nums.length == 0) return list;
            if(nums.length == 1){
                sb.append(nums[0]);
                list.add(sb.toString());
                return list;
            }
            
            for(int i = 0; i < nums.length; i++){
                int temp = i;
                sb = new StringBuffer();
                sb.append(nums[i]);
                for(int j = i; j < nums.length; j++){
                    if(nums[j] - nums[i] == 1){
                        i = j;
                    }
                }
                if(temp != i){
                    sb.append("->");
                    sb.append(nums[i]);
                }
                list.add(sb.toString());
            }
            
            return list;
        }
    }

Log in to reply
 

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