Skip to content

Commit

Permalink
0163-missing-ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 20, 2018
1 parent d56d58a commit 0f825f1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 0163-missing-ranges.playground/Contents.swift
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()
4 changes: 4 additions & 0 deletions 0163-missing-ranges.playground/contents.xcplayground
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>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ You can visit the pages below to search problems by company tags. Then come back
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
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
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
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
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
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
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
Expand Down

0 comments on commit 0f825f1

Please sign in to comment.