-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem389.swift
54 lines (47 loc) · 1.74 KB
/
problem389.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Dispatch
extension String {
func sum() -> UInt32 {
var summation = UInt32(0)
for char in self.unicodeScalars {
summation += char.value
}
return summation
}
//much slower than sum() since sum2() converts String.UnicodeScalarView to [UInt32] before summation
func sum2() -> UInt32 {
return self.unicodeScalars.map{$0.value}.reduce(0,+)
}
}
class Solution1 {
func findTheDifference(_ s: String, _ t: String) -> Character {
return Character(Unicode.Scalar((t.sum() - s.sum()))!)
}
}
//about 30 times slower than Solution1. Sorting is time-consuming.
class Solution2 {
func findTheDifference(_ s: String, _ t: String) -> Character {
let s_array = s.sorted()
let t_array = t.sorted()
for index in 0..<s_array.count {
if s_array[index] != t_array[index] {
return t_array[index]
}
}
return t_array.last!
}
}
protocol sol {
func findTheDifference(_ s: String, _ t: String) -> Character
}
extension Solution1: sol{}
extension Solution2: sol{}
func time_consuming<T: sol>(_ solution_class: T, _ name: String, _ r: Int) {
let time_start = DispatchTime.now().uptimeNanoseconds
for _ in 1...r {
_ = solution_class.findTheDifference("sjdklfwequirojaiunjkdbnakjlhquiewhtuashlkjdbvjakldshquiehgqwiglhaajksdbnvakldnvjnqiluhtqiuwehqilheaksjdbkqlehqui","sjdklfwequirojdaiunjkdbnakjlhquiewhtuashlkjdbvjakldshquiehgqwiglhaajksdbnvakldnvjnqiluhtqiuwehqilheaksjdbkqlehqui")
}
let time_end = DispatchTime.now().uptimeNanoseconds
print(name,(time_end-time_start)/1000000,"ms","for \(r) times")
}
time_consuming(Solution1(),"1",10000000)
time_consuming(Solution2(),"2",10000000)