| | | 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 | | namespace LeetCode.Algorithms.CompareVersionNumbers; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed 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) |
| | 8 | 25 | | { |
| | 8 | 26 | | var version1Index = 0; |
| | 8 | 27 | | var version2Index = 0; |
| | | 28 | | |
| | 122 | 29 | | while (version1Index < version1.Length || version2Index < version2.Length) |
| | 118 | 30 | | { |
| | 236 | 31 | | int num1 = 0, num2 = 0; |
| | | 32 | | |
| | 138 | 33 | | while (version1Index < version1.Length && version1[version1Index] != '.') |
| | 20 | 34 | | { |
| | 20 | 35 | | num1 = (num1 * 10) + (int)char.GetNumericValue(version1[version1Index]); |
| | | 36 | | |
| | 20 | 37 | | version1Index++; |
| | 20 | 38 | | } |
| | | 39 | | |
| | 254 | 40 | | while (version2Index < version2.Length && version2[version2Index] != '.') |
| | 136 | 41 | | { |
| | 136 | 42 | | num2 = (num2 * 10) + (int)char.GetNumericValue(version2[version2Index]); |
| | | 43 | | |
| | 136 | 44 | | version2Index++; |
| | 136 | 45 | | } |
| | | 46 | | |
| | 118 | 47 | | if (num1 > num2) |
| | 1 | 48 | | { |
| | 1 | 49 | | return 1; |
| | | 50 | | } |
| | | 51 | | |
| | 117 | 52 | | if (num1 < num2) |
| | 3 | 53 | | { |
| | 3 | 54 | | return -1; |
| | | 55 | | } |
| | | 56 | | |
| | 114 | 57 | | version1Index++; |
| | 114 | 58 | | version2Index++; |
| | 114 | 59 | | } |
| | | 60 | | |
| | 4 | 61 | | return 0; |
| | 8 | 62 | | } |
| | | 63 | | } |