Skip to content

Commit

Permalink
[Math] Add a solution to Fraction to Recurring Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Oct 5, 2019
1 parent 36c1931 commit 1189b4a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions Math/FractionToRecurringDecimal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/)| [Swift](./Math/SuperUglyNumber.swift)| Medium| O(n^2)| O(n)|
[Count Primes](https://leetcode.com/problems/count-primes/)| [Swift](./Math/CountPrimes.swift)| Easy| O(n)| O(n)|
[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)| [Swift](./Math/Atoi.swift)| Easy| O(n)| O(1)|
[Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/)| [Swift](FractionToRecurringDecimal.swift) | Medium| O(logn)| O(n)|
[Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/)| [Swift](./Math/FractionToRecurringDecimal.swift) | Medium| O(logn)| O(n)|
[Pow(x, n)](https://leetcode.com/problems/isomorphic-strings/)| [Swift](./Math/Pow.swift)| Medium| O(logn)| O(1)|
[Power of Two](https://leetcode.com/problems/power-of-two/)| [Swift](./Math/PowerTwo.swift)| Easy| O(1)| O(1)|
[Power of Three](https://leetcode.com/problems/power-of-three/)| [Swift](./Math/PowerThree.swift)| Easy| O(1)| O(1)|
Expand Down Expand Up @@ -705,7 +705,7 @@
| [Swift](./Array/MajorityElement.swift) | 169 | [Majority Element](https://oj.leetcode.com/problems/majority-element/) | Easy |
| | 168 | [Excel Sheet Column Title](https://oj.leetcode.com/problems/excel-sheet-column-title/) | Easy |
| | 167 | [Two Sum II - Input array is sorted](https://oj.leetcode.com/problems/two-sum-ii-input-array-is-sorted/) &hearts; | Medium |
| | 166 | [Fraction to Recurring Decimal](https://oj.leetcode.com/problems/fraction-to-recurring-decimal/) | Medium |
| [Swift](./Math/FractionToRecurringDecimal.swift) | 166 | [Fraction to Recurring Decimal](https://oj.leetcode.com/problems/fraction-to-recurring-decimal/) | Medium |
| | 165 | [Compare Version Numbers](https://oj.leetcode.com/problems/compare-version-numbers/) | Easy |
| | 164 | [Maximum Gap](https://oj.leetcode.com/problems/maximum-gap/) | Hard |
| [Swift](./Array/MissingRanges.swift) | 163 | [Missing Ranges](https://oj.leetcode.com/problems/missing-ranges/) &hearts; | Medium |
Expand Down

0 comments on commit 1189b4a

Please sign in to comment.