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;
}
相关推荐
TimberWill5 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit5 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung5 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken6 小时前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
南棱笑笑生7 小时前
20251224给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时确认ssh服务【内置dropbear】
linux·c语言·ssh·rockchip
晨晖29 小时前
顺序查找:c语言
c语言·开发语言·算法
LYFlied9 小时前
【每日算法】LeetCode 64. 最小路径和(多维动态规划)
数据结构·算法·leetcode·动态规划
sin_hielo10 小时前
leetcode 3074
数据结构·算法·leetcode
程序员-King.11 小时前
day124—二分查找—最小化数组中的最大值(LeetCode-2439)
算法·leetcode·二分查找
阿华hhh11 小时前
Linux系统编程(网络udp)
linux·服务器·c语言·网络·网络协议·udp