C语言 | Leetcode C语言题解之第165题比较版本号

题目:

题解:

cpp 复制代码
int compareVersion(char * version1, char * version2){
    int len1 = strlen(version1);
    int len2 = strlen(version2);
    int i = 0;
    int j = 0;
    while (i < len1 || j < len2) {
        int num1 = 0;
        int num2 = 0;
        while (i < len1 && version1[i] != '.') {
            num1 = num1 * 10 + (version1[i++] - '0');
        }
        while (j < len2 && version2[j] != '.') {
            num2 = num2 * 10 + (version2[j++] - '0');
        }
        if (num1 < num2) {
            return -1;
        } else if (num1 > num2) {
            return 1;
        }
        i++;
        j++;
    }
    return 0;
}
相关推荐
wuyk5557 小时前
Keil工具链-第05讲-启动文件与__main到底做了什么
c语言·stm32·单片机·嵌入式硬件·物联网
文丑颜不良啊15 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z15 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
冻柠檬飞冰走茶16 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊17 小时前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊18 小时前
LeetCode2141~2160题目解析
java·leetcode
Hhy_110720 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
曦樂~1 天前
Cpp面筋
c语言·c++
Dream it possible!1 天前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊1 天前
算法之双指针系列(一)
算法·leetcode