【leetcode】165. 比较版本号

 

int compareVersion(char * version1, char * version2){
    int i, j, start1, start2, num1, num2, len1 = strlen(version1), len2 = strlen(version2);
    for (i = 0, j = 0; i<len1 || j<len2;)
    {
        start1 = i; start2 = j;
        while (i < len1 && version1[i] != '.')
            i++;
        if (i<len1)
            version1[i] = '\0';
        while (j < len2 && version2[j] != '.')
            j++;
        if (j<len2)
            version2[j] = '\0';

        num1 = (start1<len1) ? atoi(version1 + start1) : 0;
        num2 = (start2<len2) ? atoi(version2 + start2) : 0;

        if (num1 != num2)
            return (num1 > num2) ? 1 : -1;
        i++; j++;
    }
    return 0;
}

 

posted @ 2020-12-24 22:01  温暖了寂寞  阅读(76)  评论(0编辑  收藏  举报