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->99"].

 1 public List<String> findMissingRanges(int[] nums, int lower, int upper) {
 2     List<String> list = new ArrayList<String>();
 3     if (nums == null)
 4         return list;
 5
 6     if (nums.length == 0) {
 7         list.add(String.valueOf(lower) + "->" + String.valueOf(upper));
 8     }
 9
10     for (int i = 0; i < nums.length - 1; i++) {
11         if (nums[i] + 2 < nums[i + 1]) {
12             list.add(String.valueOf(nums[i] + 1) + "->" + String.valueOf(nums[i + 1] - 1));
13         } else if (nums[i] + 2 == nums[i + 1]) {
14             list.add(String.valueOf(nums[i] + 1));
15         }
16     }
17
18     if (lower + 1 == nums[0]) {
19         list.add(0, String.valueOf(lower + 1));
20     } else if (lower + 1 < nums[0]) {
21         list.add(0, String.valueOf(lower) + "->" + String.valueOf(nums[0] - 1));
22     }
23
24     if (nums[nums.length - 1] + 1 == upper) {
25         list.add(String.valueOf(upper - 1));
26     } else if (nums[nums.length - 1] + 1 < upper) {
27         list.add(String.valueOf(nums[nums.length - 1] + 1) + "->" + String.valueOf(upper));
28     }
29     return list;
30 }
时间: 2025-01-14 07:50:32

Missing Ranges的相关文章

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-

[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] Missing Ranges

Problem Description: 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-&g

[Locked] 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

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", "

Missing Ranges 解答

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

Missing Ranges -- LeetCode

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-

【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

LeetCode Missing Ranges [LeetCode Book Problem]

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-