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;
}
相关推荐
练习时长一年2 小时前
Leetcode热题100(跳跃游戏 II)
算法·leetcode·游戏
小白菜又菜7 小时前
Leetcode 3432. Count Partitions with Even Sum Difference
算法·leetcode
wuhen_n8 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo8 小时前
leetcode 2483
数据结构·算法·leetcode
sevenez9 小时前
Vibe Coding 实战笔记:从“修好了C坏了AB”到企业级数据库架构重构
c语言·笔记·数据库架构
一路往蓝-Anbo9 小时前
【第20期】延时的艺术:HAL_Delay vs vTaskDelay
c语言·数据结构·stm32·单片机·嵌入式硬件
wuhen_n11 小时前
LeetCode -- 1:两数之和(简单)
javascript·算法·leetcode·职场和发展
就不掉头发11 小时前
I/O复用
运维·服务器·c语言·开发语言
Jeremy爱编码12 小时前
leetcode课程表
算法·leetcode·职场和发展
努力学算法的蒟蒻13 小时前
day46(12.27)——leetcode面试经典150
算法·leetcode·面试