当前位置:网站首页>Leetcode165 compare version number double pointer string

Leetcode165 compare version number double pointer string

2022-04-23 14:49:00 Li Fan, hurry up

Original link
 Insert picture description here

Note:

Just pick out the numbers of each position and compare them with the size , Pay attention to fully buckle , Maybe both of them are the same in front , however 1 More points , Namely 1 Longer , It means that 1 It's big

The code is as follows :

class Solution {
    
public:
    int compareVersion(string version1, string version2) {
    
        int i = 0, j = 0;
        while(i < version1.size() || j < version2.size()){
    
            string s1 = "0", s2 = "0";
            while(i < version1.size() && version1[i] != '.')
                s1 += version1[i ++];
            while(j < version2.size() && version2[j] != '.')
                s2 += version2[j ++];
            int v1 = stoi(s1), v2 = stoi(s2);
            if(v1 > v2) return 1;
            else if(v1 < v2)    return -1;
            while(version1[i] == '.')   i ++;
            while(version2[j] == '.')   j ++;
        }

        return 0;
    }
};

版权声明
本文为[Li Fan, hurry up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231447599623.html