-
Notifications
You must be signed in to change notification settings - Fork 887
/
FractionToRecurringDecimal.swift
46 lines (38 loc) · 1.49 KB
/
FractionToRecurringDecimal.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
/**
* Question Link: https://leetcode.com/problems/fraction-to-recurring-decimal/
* Primary idea: Get quotient and remainder, if remainder exists, then it is repeating.
* Time Complexity: O(logn), Space Complexity: O(n)
*
*/
class FractionToRecurringDecimal {
func fractionToDecimal(_ numerator: Int, _ denominator: Int) -> String {
guard numerator != 0 else {
return "0"
}
guard denominator != 0 else {
fatalError("Invalid Denominator")
}
var isPositive = (numerator < 0) == (denominator < 0), numerator = abs(numerator), res = "", numToIndex = [Int: Int](), hasDot = false
let denominator = abs(denominator)
while numerator != 0 {
let (q, r) = numerator.quotientAndRemainder(dividingBy: denominator)
if let numIndex = numToIndex[numerator]{
res.insert("(", at: res.index(res.startIndex, offsetBy: numIndex))
res.append(")")
break
} else {
res += "\(q)"
if !hasDot && r != 0 {
res.append(".")
hasDot = true
} else {
// update numToIndex
numToIndex[numerator] = res.count - 1
}
// update reminder
numerator = r * 10
}
}
return isPositive ? res : "-" + res
}
}