< Summary

Information
Class: LeetCode.Algorithms.CompareVersionNumbers.CompareVersionNumbersTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CompareVersionNumbers\CompareVersionNumbersTwoPointers.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CompareVersion(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CompareVersionNumbers\CompareVersionNumbersTwoPointers.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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
 12namespace LeetCode.Algorithms.CompareVersionNumbers;
 13
 14/// <inheritdoc />
 15public class CompareVersionNumbersTwoPointers : ICompareVersionNumbers
 16{
 17    /// <summary>
 18    ///     Time complexity - O(max(n,m)), where n and m are the lengths of version1 and version2 respectively
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="version1"></param>
 22    /// <param name="version2"></param>
 23    /// <returns></returns>
 24    public int CompareVersion(string version1, string version2)
 825    {
 826        var version1Index = 0;
 827        var version2Index = 0;
 28
 12229        while (version1Index < version1.Length || version2Index < version2.Length)
 11830        {
 23631            int num1 = 0, num2 = 0;
 32
 13833            while (version1Index < version1.Length && version1[version1Index] != '.')
 2034            {
 2035                num1 = (num1 * 10) + (int)char.GetNumericValue(version1[version1Index]);
 36
 2037                version1Index++;
 2038            }
 39
 25440            while (version2Index < version2.Length && version2[version2Index] != '.')
 13641            {
 13642                num2 = (num2 * 10) + (int)char.GetNumericValue(version2[version2Index]);
 43
 13644                version2Index++;
 13645            }
 46
 11847            if (num1 > num2)
 148            {
 149                return 1;
 50            }
 51
 11752            if (num1 < num2)
 353            {
 354                return -1;
 55            }
 56
 11457            version1Index++;
 11458            version2Index++;
 11459        }
 60
 461        return 0;
 862    }
 63}