[LeetCode] 163. Missing Ranges 缺失区间

Given a sorted integer array nums, where the range of elements are in the inclusive range [lowerupper], return its missing ranges.

Example:

Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
Output: ["2", "4->49", "51->74", "76->99"]

Java:

class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> res = new ArrayList<String>();
        int next = lower;
        for (int i = 0; i < nums.length; i++) {
            // 1. We don't need to add [Integer.MAX_VALUE, ...] to result
            if(lower == Integer.MAX_VALUE) return res;
            if (nums[i] < next) {
                continue;
            }
            if (nums[i] == next) {
                next++;
                continue;
            }
            res.add(getRange(next, nums[i] - 1));
            // 2. We don't need to proceed after we have process Integer.MAX_VALUE in array
            if(nums[i] == Integer.MAX_VALUE) return res;
            next = nums[i] + 1;
        }
        
        if (next <= upper) {
            res.add(getRange(next, upper));
        }
        return res;
    }
    
    public String getRange(int n1, int n2) {
        return n1 == n2 ? String.valueOf(n1) : String.format("%d->%d" , n1, n2);
    }
        
}  

Java:

public class Solution {
    public List<String> findMissingRanges(int[] A, int lower, int upper) {
        if(A==null) return null;
        List<String> res = new ArrayList<String>();
        for(int i=0; i<A.length; i++) {
            while(i<A.length && A[i] == lower ) {lower++; i++;} 
            if(i>=A.length) break;
            if(A[i] == lower+1) {
                res.add(String.valueOf(lower));
            } else {
                res.add("" + lower + "->" + (A[i]-1) );
            }
            lower = A[i] + 1;
        }
        
        if(lower == upper) {
            res.add(String.valueOf(lower));
        } else if(lower < upper ){
            res.add("" + lower + "->" + upper );
        }
        return res;
    }
}  

Java:

public class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> res = new ArrayList<>();
        if (nums == null || lower > upper) return res;
        for (int num : nums) {
            if (num - lower >= 1) res.add(getRangeString(lower, num - 1));
            lower = num + 1;
        }
        if (lower <= upper) res.add(getRangeString(lower, upper));
        return res;
    }
    
    private String getRangeString(int lower, int upper) {
        if (lower == upper) return String.valueOf(lower);
        StringBuilder sb = new StringBuilder();
        sb.append(lower).append("->").append(upper);
        return sb.toString();
    }
}  

Python:

class Solution(object):
    def findMissingRanges(self, nums, lower, upper):
        """
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: List[str]
        """
        def getRange(lower, upper):
            if lower == upper:
                return "{}".format(lower)
            else:
                return "{}->{}".format(lower, upper)
        ranges = []
        pre = lower - 1

        for i in xrange(len(nums) + 1):
            if i == len(nums):
                cur = upper + 1
            else:
                cur = nums[i]
            if cur - pre >= 2:
                ranges.append(getRange(pre + 1, cur - 1))

            pre = cur

        return ranges  

C++:

class Solution {
public:
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> res;
        int l = lower;
        for (int i = 0; i <= nums.size(); ++i) {
            int r = (i < nums.size() && nums[i] <= upper) ? nums[i] : upper + 1;
            if (l == r) ++l;
            else if (r > l) {
                res.push_back(r - l == 1 ? to_string(l) : to_string(l) + "->" + to_string(r - 1));
                l = r + 1;
            }
        }
        return res;
    }
};

  

类似题目:

[LeetCode] 228. Summary Ranges 总结区间

 

All LeetCode Questions List 题目汇总

posted @ 2018-09-13 14:49  轻风舞动  阅读(1244)  评论(0编辑  收藏  举报