228. Summary Ranges

题目:

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

链接: http://leetcode.com/problems/summary-ranges/

题解:

总结Range。也是从头到尾走一遍。当nums[i] - 1> nums[i - 1],我们处理之前的数字们。当遍历到最后一个元素的时候也要考虑如何处理。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if(nums == null || nums.length == 0)
            return res;
        int lo = 0;
        StringBuilder sb = new StringBuilder();
        
        for(int i = 0; i < nums.length; i++) {
            if(i > 0 && (nums[i] - 1 > nums[i - 1])) {
                if(lo == i - 1)
                    res.add(Integer.toString(nums[lo]));
                else {
                    res.add(sb.append(nums[lo]).append("->").append(nums[i - 1]).toString());
                    sb.setLength(0);
                }
                lo = i;
            }
            
            if(i == nums.length - 1) {
                if(lo == i)
                    res.add(Integer.toString(nums[lo]));
                else
                    res.add(sb.append(nums[lo]).append("->").append(nums[i]).toString());
            }
        }
        
        return res;
    }
}

 

二刷:

  1. 方法和一刷一样。我们主要使用一个变量lo来保存每个interval的左边界。
  2. 每次当 i > 0并且 nums[i] - 1 > nums[i]的时候,我们进行判断
    1. 假如 lo = i - 1,那么我们只有一个数字,直接把这个数字加入到结果
    2. 否则 我们要把 nums[lo] + "->" + nums[i - 1]这个字符串加入到结果
  3. 更新 lo = i
  4. 当i = len - 1的时候,我们也要根据上面的逻辑判断一遍
  5. 最后返回结果.

Java:

可以使用StringBuilder来减少空间复杂度。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        int lo = 0, len = nums.length;
        for (int i = 0; i < len; i++) {
            if (i > 0 && (nums[i] - 1 > nums[i - 1])) {
                if (lo == i - 1) {
                    res.add(nums[lo]  + "");
                } else {
                    res.add(nums[lo]  + "->" + nums[i - 1]);
                }
                lo = i;
            }
            if (i == len - 1) {
                if (lo == len - 1) {
                    res.add(nums[lo] + "");
                } else {
                    res.add(nums[lo] + "->" + nums[len - 1]);
                }
            }
        }
        return res;
    }
}

 

三刷:

不知道上面在写什么...这回主要使用一个变量count和一个StringBuilder sb。遍历整个数组,当count = 0的时候,我们在sb中加入当前nums[i]。 当nums[i] - nums[i - 1]时,我们增加count。否则,这时nums[i] - nums[i - 1] > 1,假如count > 1,则形成了一个range,我们在sb中append一个符号"->",再append上一个数字,把sb输出到结果。否则count = 1,我们直接输出sb到结果。  最后运行完毕时假如count仍然>0,我们做相应步骤,把sb输出到结果。

Java:

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        StringBuilder sb = new StringBuilder();
        int count = 0;
        
        for (int i = 0; i < nums.length; i++) {
            if (count == 0) {
                sb.append(nums[i]);
                count++;
            } else if (nums[i] - nums[i - 1] == 1) {
                count++;
            } else {
                if (count > 1) sb.append("->").append(nums[i - 1]);
                res.add(sb.toString());
                sb.setLength(0);
                sb.append(nums[i]);
                count = 1;
            }
        }
        
        if (count > 1) sb.append("->").append(nums[nums.length - 1]);
        res.add(sb.toString());
        return res;
    }
}

 

简化一下。分析的时候要考虑起始状态,过程分支以及结束时的边界条件。

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        StringBuilder sb = new StringBuilder(nums[0] + "");
        int count = 1;
        
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] == 1) {
                count++;
            } else {
                if (count > 1) sb.append("->").append(nums[i - 1]);
                res.add(sb.toString());
                sb.setLength(0);
                sb.append(nums[i]);
                count = 1;
            }
        }
        
        if (count > 1) sb.append("->").append(nums[nums.length - 1]);
        res.add(sb.toString());
        return res;
    }
}

 

Update:

Different logic. This time we use a sliding window. If we found out nums[i] > nums[i - 1] + 1, we need to add result to res.  here if i - 1 != lo, we need to add a range into res,  otherwise we need to add a single number into res.  We also need to do double check while we finished running the loop. 

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        StringBuilder sb = new StringBuilder();
        int lo = 0, len = nums.length;
        for (int i = 1; i < len; i++) {
            if (nums[i] > nums[i - 1] + 1) {
                if (i - 1 != lo) sb.append(nums[lo]).append("->").append(nums[i - 1]);
                else sb.append(nums[lo]);
                res.add(sb.toString());
                lo = i;
                sb.setLength(0);
            }
        }
        if (lo == len - 1) sb.append(nums[lo]);
        else sb.append(nums[lo]).append("->").append(nums[len - 1]);
        res.add(sb.toString());
        return res;
    }
} 

 

Reference:

https://leetcode.com/discuss/42229/10-line-c-easy-understand

https://leetcode.com/discuss/42199/6-lines-in-python

https://leetcode.com/discuss/42342/idea-1-liner-group-by-number-index

https://leetcode.com/discuss/42290/accepted-java-solution-easy-to-understand

 

posted @ 2015-11-26 04:57  YRB  阅读(331)  评论(0编辑  收藏  举报