-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/// 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->74", "76->99"] | ||
|
||
import XCTest | ||
|
||
/// Approach: Iteration | ||
func findMissingRanges(_ nums: [Int], _ lower: Int, _ upper: Int) -> [String] { | ||
var numbers = [lower - 1] | ||
numbers.append(contentsOf: nums) | ||
numbers.append(upper + 1) | ||
var result: [String] = [] | ||
for i in 0..<numbers.count - 1 { | ||
let difference = numbers[i + 1] - numbers[i] | ||
guard difference > 1 else { continue } | ||
if difference == 2 { | ||
result.append(String(numbers[i] + 1)) | ||
} else { | ||
result.append(String(numbers[i] + 1) + "->" + String(numbers[i + 1] - 1)) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
class Tests: XCTestCase { | ||
func testExample() { | ||
let nums = [0, 1, 3, 50, 75] | ||
let expected = ["2", "4->49", "51->74", "76->99"] | ||
XCTAssertEqual(findMissingRanges(nums, 0, 99), expected) | ||
} | ||
} | ||
|
||
Tests.defaultTestSuite.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters