Skip to content

Commit 0f825f1

Browse files
committed
0163-missing-ranges
1 parent d56d58a commit 0f825f1

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// 163. Missing Ranges
2+
/// Given a sorted integer array nums, where the range of elements are in the inclusive range
3+
/// [lower, upper], return its missing ranges.
4+
///
5+
/// Example:
6+
/// Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
7+
/// Output: ["2", "4->49", "51->74", "76->99"]
8+
9+
import XCTest
10+
11+
/// Approach: Iteration
12+
func findMissingRanges(_ nums: [Int], _ lower: Int, _ upper: Int) -> [String] {
13+
var numbers = [lower - 1]
14+
numbers.append(contentsOf: nums)
15+
numbers.append(upper + 1)
16+
var result: [String] = []
17+
for i in 0..<numbers.count - 1 {
18+
let difference = numbers[i + 1] - numbers[i]
19+
guard difference > 1 else { continue }
20+
if difference == 2 {
21+
result.append(String(numbers[i] + 1))
22+
} else {
23+
result.append(String(numbers[i] + 1) + "->" + String(numbers[i + 1] - 1))
24+
}
25+
}
26+
return result
27+
}
28+
29+
class Tests: XCTestCase {
30+
func testExample() {
31+
let nums = [0, 1, 3, 50, 75]
32+
let expected = ["2", "4->49", "51->74", "76->99"]
33+
XCTAssertEqual(findMissingRanges(nums, 0, 99), expected)
34+
}
35+
}
36+
37+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ You can visit the pages below to search problems by company tags. Then come back
5454
33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0033-search-in-rotated-sorted-array.playground/Contents.swift) | Binary Search
5555
66 | [Plus One](https://leetcode.com/problems/plus-one/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0066-plus-one.playground/Contents.swift) | Math
5656
84 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0084-largest-rectangle-in-histogram.playground/Contents.swift) | Stack
57+
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0163-missing-ranges.playground/Contents.swift) | BFS
5758
200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0200-number-of-islands.playground/Contents.swift) | BFS
5859
482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift) | String
5960
681 | [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift) | Simulation

0 commit comments

Comments
 (0)