Leetcode - Summary Ranges

Leetcode - Summary Ranges_第1张图片

My code:

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List summaryRanges(int[] nums) {
        if (nums == null)
            return null;
        List sum = new ArrayList();
        if (nums.length == 0)
            return sum;
        else if (nums.length == 1) {
            sum.add(Integer.toString(nums[0]));
            return sum;
        }
        int begin = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1] + 1) {
                if (i == nums.length - 1) {
                    String s1 = Integer.toString(nums[begin]);
                    s1 += "->" + Integer.toString(nums[i]);
                    sum.add(s1);
                }
                continue;
            }
            else {
                String s2 = Integer.toString(nums[begin]);
                if (i - begin > 1)
                    s2 += "->" + Integer.toString(nums[i - 1]);
                sum.add(s2);
                begin = i;
                if (begin == nums.length - 1) {
                    String s = Integer.toString(nums[begin]);
                    sum.add(s);
                }
            }
        }
        return sum;
    }
    
    public static void main (String[] args) {
        Solution test = new Solution();
        int[] nums = {0,1,2,4,5,7};
        System.out.println(test.summaryRanges(nums));
    }
    
    
}

My test result:

Leetcode - Summary Ranges_第2张图片

这次作业不难。也没什么好总结的。左手给我女朋友打电话,她不接,右手敲着代码,敲着if语句。。。所以说,真的不难。

**
总结: Array
**

Anyway, Good luck, Richardo!

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List summaryRanges(int[] nums) {
        ArrayList ret = new ArrayList();
        if (nums == null || nums.length == 0)
            return ret;
        int headIndex = 0;
        int tailIndex = 1;
        while (tailIndex < nums.length) {
            if (nums[tailIndex] - nums[tailIndex - 1] == 1) {
                tailIndex++;
                continue;
            }
            else {
                String range = "";
                if (tailIndex - 1 == headIndex)
                    range = String.valueOf(nums[headIndex]);
                else
                    range = nums[headIndex] + "->" + nums[tailIndex - 1];
                ret.add(range);
                headIndex = tailIndex;
                tailIndex++;
            }
        }
        String range = "";
        if (headIndex == nums.length - 1) {
            range = String.valueOf(nums[headIndex]);
        }
        else {
            range = nums[headIndex] + "->" + nums[tailIndex - 1];
        }
        ret.add(range);
        return ret;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        int[] nums = {0, 1, 2, 4, 5, 6, 7};
        System.out.println(test.summaryRanges(nums));
    }
}

新写的代码,感觉逻辑更加顺畅些。不难。

Good luck, Richardo!

My Code:

public class Solution {
    public List summaryRanges(int[] nums) {
        List ret = new ArrayList();
        if (nums == null || nums.length == 0) {
            return ret;
        }
        
        int prev = nums[0];
        int next = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1] + 1) {
                next = nums[i];
            }
            else {
                if (prev == next) {
                    ret.add("" + prev);
                }
                else {
                    ret.add("" + prev + "->" + next);
                }
                prev = nums[i];
                next = nums[i];
            }
        }
        
        if (prev == next) {
            ret.add("" + prev);
        }
        else {
            ret.add("" + prev + "->" + next);
        }
        return ret;
    }
}

不知道为什么想出了这么简洁巧妙的算法。
很美,很简洁。

Anyway, Good luck, Richardo! -- 08/11/2016

你可能感兴趣的:(Leetcode - Summary Ranges)