/166. Fraction to Recurring Decimal

166. Fraction to Recurring Decimal

Medium
Hash Table30.6% acceptance

Given two integers, dividend and divisor, return their division as a string in decimal format. If the fractional part is repeating, enclose the repeating sequence in parentheses. If the result is a finite decimal, return it as is. Negative values should be handled correctly. The divisor is guaranteed to be non-zero.

Example 1

Input: dividend = 7, divisor = 12

Output: 0.58(3)

Explanation: 7/12 = 0.583333..., so the repeating part is 3.

Example 2

Input: dividend = -10, divisor = 3

Output: -3.(3)

Explanation: -10/3 = -3.333..., so the repeating part is 3.

Example 3

Input: dividend = 0, divisor = 5

Output: 0

Explanation: 0 divided by any non-zero number is 0.

Constraints

  • -231 <= dividend <= 231 - 1
  • -231 <= divisor <= 231 - 1
  • divisor != 0
  • The output string length is less than 104
Python (current runtime)

Case 1

Input: dividend = 5, divisor = 8

Expected: 0.625

Case 2

Input: dividend = 22, divisor = 7

Expected: 3.(142857)

Case 3

Input: dividend = -15, divisor = 4

Expected: -3.75

Case 4

Input: dividend = 1, divisor = 6

Expected: 0.1(6)

Case 5

Input: dividend = 100, divisor = 11

Expected: 9.(09)