JAVA-----------------Easy Version To Understand!!!!!!!!!!!!!


  • 1
    H
    public static List<String> summaryRanges(int[] nums) {
    	if (nums == null || nums.length == 0)
    		return new ArrayList<String>();
    	List<String> result = new ArrayList<String>();
    	int len = nums.length, low = 0, high = 0;//
    	while (high < len) {
    		if (high + 1 < len && nums[high] + 1 == nums[high + 1])
    			high++;
    		else {
    			if (low == high) {
    				result.add(String.valueOf(nums[low]));
    			} else {
    				result.add(nums[low] + "->" + nums[high]);
    			}
    			high++;
    			low = high;
    		}
    	}
    	return result;
    }

Log in to reply
 

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