My CSharp solution


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

Log in to reply
 

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