美文网首页
165. Compare Version Numbers

165. Compare Version Numbers

作者: larrymusk | 来源:发表于2017-12-01 19:53 被阅读0次
    int compareVersion(char* version1, char* version2) {  
        int lenversion1 = strlen(version1);  
        int lenversion2 = strlen(version2);  
        int i = 0;  
        int j = 0;  
        int num1 = 0;  
        int num2 = 0;  
      
        while (i < lenversion1 || j < lenversion2)  
        {  
            while (i < lenversion1&&version1[i] != '.')  
            {  
                num1 = 10 * num1 + version1[i]-'0';  
                i++;  
            }  
      
            while (j < lenversion2 && version2[j] != '.')  
            {  
                num2 = 10 * num2 + version2[j]-'0';  
                j++;  
            }  
    //先比较小数点之前的数字
      
            if (num1>num2)return 1;  
            else if (num1 < num2)return -1;  
    
    //如果小数点前面数字相等 就比较小数点后面的数字
            i++;  
            j++;  
            num1 = 0;  
            num2 = 0;  
        }  
        return 0;  
    }  

    相关文章

      网友评论

          本文标题:165. Compare Version Numbers

          本文链接:https://www.haomeiwen.com/subject/bsawbxtx.html