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 0f825f1 commit 450f165
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions 0163-missing-ranges.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ 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]
for i in -1..<nums.count {
let current = i == -1 ? lower - 1 : nums[i]
let next = i + 1 < nums.count ? nums[i + 1] : upper + 1
let difference = next - current
guard difference > 1 else { continue }
if difference == 2 {
result.append(String(numbers[i] + 1))
result.append(String(current + 1))
} else {
result.append(String(numbers[i] + 1) + "->" + String(numbers[i + 1] - 1))
result.append(String(current + 1) + "->" + String(next - 1))
}
}
return result
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +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
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) | Iteration
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 450f165

Please sign in to comment.