< Summary

Information
Class: LeetCode.Algorithms.FractionToRecurringDecimal.FractionToRecurringDecimalDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FractionToRecurringDecimal\FractionToRecurringDecimalDictionary.cs
Line coverage
84%
Covered lines: 27
Uncovered lines: 5
Coverable lines: 32
Total lines: 80
Line coverage: 84.3%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FractionToDecimal(...)68.75%171684.37%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FractionToRecurringDecimal\FractionToRecurringDecimalDictionary.cs

#LineLine coverage
 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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.FractionToRecurringDecimal;
 15
 16/// <inheritdoc />
 17public 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)
 333    {
 334        if (numerator == 0)
 035        {
 036            return Zero;
 37        }
 38
 339        var stringBuilder = new StringBuilder();
 40
 341        if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0))
 042        {
 043            stringBuilder.Append(Minus);
 044        }
 45
 346        var dividend = Math.Abs((long)numerator);
 347        var divisor = Math.Abs((long)denominator);
 48
 349        stringBuilder.Append(dividend / divisor);
 50
 351        var remainder = dividend % divisor;
 52
 353        if (remainder == 0)
 154        {
 155            return stringBuilder.ToString();
 56        }
 57
 258        stringBuilder.Append(DecimalPoint);
 59
 260        var remainderToIndex = new Dictionary<long, int>();
 61
 662        while (remainder != 0)
 563        {
 564            if (remainderToIndex.TryGetValue(remainder, out var index))
 165            {
 166                stringBuilder.Insert(index, OpeningParenthesis);
 167                stringBuilder.Append(ClosingParenthesis);
 68
 169                break;
 70            }
 71
 472            remainderToIndex[remainder] = stringBuilder.Length;
 473            remainder *= 10;
 474            stringBuilder.Append(remainder / divisor);
 475            remainder %= divisor;
 476        }
 77
 278        return stringBuilder.ToString();
 379    }
 80}