C# solution, easy to understand


  • 0
    I
    public int CompareVersion(string version1, string version2)
    {
        if (version1 == version2)
            return 0;
    
        var v1Ary = version1.Split('.');
        var v2Ary = version2.Split('.');
              
        int maxLen = v1Ary.Length >= v2Ary.Length ? v1Ary.Length : v2Ary.Length;
        for (int i = 0; i < maxLen; i++)
        {
            int v1 = (v1Ary.Length <= i) ? 0 : int.Parse(v1Ary[i]);
            int v2 = (v2Ary.Length <= i) ? 0 : int.Parse(v2Ary[i]);
    
            if (v1 == v2)
                continue;
    
            return (v1 > v2) ? 1 : -1;
        }
    
        return 0;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.