Skip to content

Commit

Permalink
0681-next-closest-time
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 18, 2018
1 parent 15d0827 commit 60fbfd0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 0681-next-closest-time.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// 681. Next Closest Time
/// Given a time represented in the format "HH:MM", form the next closest time by reusing the
/// current digits. There is no limit on how many times a digit can be reused.
/// You may assume the given input string is always valid. For example, "01:34", "12:09" are all
/// valid. "1:34", "12:9" are all invalid.

import XCTest

/// Approach: String
func nextClosestTime(_ time: String) -> String {
guard time.count == 5 else { fatalError() }
let timeChars = Array(time)
let hh = Int(String(timeChars[0..<2]))!
let mm = Int(String(timeChars[3..<5]))!
var m = (mm + 1) % 60
var h = m == 0 ? (hh + 1) % 24 : hh
let timeSet = Set(timeChars)
while h != hh || m != mm {
let hStr = h > 9 ? String(h) : "0" + String(h)
let mStr = m > 9 ? String(m) : "0" + String(m)
let nextTime = hStr + ":" + mStr
if Set(Array(nextTime)).isSubset(of: timeSet) {
return nextTime
}
m = (m + 1) % 60
h = m == 0 ? (h + 1) % 24 : h
}
return time
}

class Tests: XCTestCase {
func testExample() {
let time = "19:34"
XCTAssertEqual(nextClosestTime(time), "19:39")
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0681-next-closest-time.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 @@ -51,3 +51,4 @@ You can visit the pages below to search problems by company tags. Then come back
31 | [Next Permutation](https://leetcode.com/problems/next-permutation/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0031-next-permutation.playground/Contents.swift) | Two Pointers
32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0032-longest-valid-parentheses.playground/Contents.swift) | Stack
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
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 comments on commit 60fbfd0

Please sign in to comment.