[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 总结区间

原文地址:https://www.cnblogs.com/lightwindy/p/9640454.html

时间: 2024-10-06 07:46:35

[LeetCode] 163. Missing Ranges 缺失区间的相关文章

[LeetCode] Missing Ranges 缺失区间

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 这道题让我们求缺失区间,跟之前那道Summary Ranges很类似,这道题让我们求缺失的空间,给了一个空间的范围[lo

leetcode[163]Missing Ranges

Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76-

163. Missing Ranges

题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数

【LeetCode】Missing Ranges

Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74&q

LC 163. Missing Ranges

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99, Output: ["2", "4->49", "51

[LeetCode]71. Missing Number缺失的数

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi

[LeetCode] 268. Missing Number 缺失的数字

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1 Input: [3,0,1] Output: 2 Example 2 Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear runtime c

LeetCode – Refresh – Missing Ranges

1 class Solution { 2 public: 3 string getRange(int start, int end) { 4 ostringstream oss; 5 if (start == end) { 6 oss << start; 7 } else { 8 oss << start << "->" << end; 9 } 10 return oss.str(); 11 } 12 vector<strin