| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | using System.Text; |
| | | 13 | | |
| | | 14 | | namespace LeetCode.Algorithms.FractionToRecurringDecimal; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public sealed class FractionToRecurringDecimalDictionary : IFractionToRecurringDecimal |
| | | 18 | | { |
| | | 19 | | private const string Zero = "0"; |
| | | 20 | | private const char Minus = '-'; |
| | | 21 | | private const char DecimalPoint = '.'; |
| | | 22 | | private const char OpeningParenthesis = '('; |
| | | 23 | | private const char ClosingParenthesis = ')'; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Time complexity - O(n), where n is the number of digits in the resulting decimal representation |
| | | 27 | | /// Space complexity - O(n), where n is the number of digits in the resulting decimal representation |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="numerator"></param> |
| | | 30 | | /// <param name="denominator"></param> |
| | | 31 | | /// <returns></returns> |
| | | 32 | | public string FractionToDecimal(int numerator, int denominator) |
| | 3 | 33 | | { |
| | 3 | 34 | | if (numerator == 0) |
| | 0 | 35 | | { |
| | 0 | 36 | | return Zero; |
| | | 37 | | } |
| | | 38 | | |
| | 3 | 39 | | var stringBuilder = new StringBuilder(); |
| | | 40 | | |
| | 3 | 41 | | if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0)) |
| | 0 | 42 | | { |
| | 0 | 43 | | stringBuilder.Append(Minus); |
| | 0 | 44 | | } |
| | | 45 | | |
| | 3 | 46 | | var dividend = Math.Abs((long)numerator); |
| | 3 | 47 | | var divisor = Math.Abs((long)denominator); |
| | | 48 | | |
| | 3 | 49 | | stringBuilder.Append(dividend / divisor); |
| | | 50 | | |
| | 3 | 51 | | var remainder = dividend % divisor; |
| | | 52 | | |
| | 3 | 53 | | if (remainder == 0) |
| | 1 | 54 | | { |
| | 1 | 55 | | return stringBuilder.ToString(); |
| | | 56 | | } |
| | | 57 | | |
| | 2 | 58 | | stringBuilder.Append(DecimalPoint); |
| | | 59 | | |
| | 2 | 60 | | var remainderToIndex = new Dictionary<long, int>(); |
| | | 61 | | |
| | 6 | 62 | | while (remainder != 0) |
| | 5 | 63 | | { |
| | 5 | 64 | | if (remainderToIndex.TryGetValue(remainder, out var index)) |
| | 1 | 65 | | { |
| | 1 | 66 | | stringBuilder.Insert(index, OpeningParenthesis); |
| | 1 | 67 | | stringBuilder.Append(ClosingParenthesis); |
| | | 68 | | |
| | 1 | 69 | | break; |
| | | 70 | | } |
| | | 71 | | |
| | 4 | 72 | | remainderToIndex[remainder] = stringBuilder.Length; |
| | 4 | 73 | | remainder *= 10; |
| | 4 | 74 | | stringBuilder.Append(remainder / divisor); |
| | 4 | 75 | | remainder %= divisor; |
| | 4 | 76 | | } |
| | | 77 | | |
| | 2 | 78 | | return stringBuilder.ToString(); |
| | 3 | 79 | | } |
| | | 80 | | } |