【一天一道LeetCode】#165. Compare Version Numbers.md

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源: https://leetcode.com/problems/compare-version-numbers/

Compare two version numbers version1 and version2.

If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character.

The . character does not represent a decimal point and is used to separate number sequences.

For instance, 2.5 is not “two and a half” or “half way to version three”, it is the fifth second-level revision of the second first-level >revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

(二)解题

题目大意:给定两个版本号码,比较他们的大小。

解题思路:版本号一般为1.1.2,中间以点(.)隔开,要判断版本号的大小,需要计算每个以点隔开的数的大小。

需要注意一些特殊情况:

(1) 00001.1.1这种前面有0干扰的

(2) 1.0.0.0.0和1是相等的

详细思路见如下代码和注释:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int len1= version1.length();
        int len2= version2.length();
        int i = 0;
        int j = 0;
        while(i<len1&&j<len2)//首先计算两个版本号相同位置的数的大小
        {
            int num1 =countnum(version1,i);
            int num2 =countnum(version2,j);
            i++;j++;
            if(num1==num2) continue;
            else return num1>num2?1:-1;
        }
        if(i>=len1&&j<len2)//如果version1比较完了,则判断version2后面是否全为0
        {
            while(j<len2)
            {
                int num2 =countnum(version2,j);
                j++;
                if(num2==0) continue;//等于0就继续往后
                else return -1;//否则就返回version2大
            }
            return 0;//全为0就返回相等
        }
        if(i<len1&&j>=len2)//同上,如果version2比较完了,则判断version1后面是否全为0
        {
            while(i<len1)
            {
                int num1 =countnum(version1,i);
                i++;
                if(num1==0) continue;//等于0就继续往后判断
                else return 1;//否则就返回version1大
            }
            return 0;//全为0就返回相等
        }
        return 0;//上述比较都相等就返回相等
    }
    int countnum(string s , int& i)//用来计算以点隔开的数字
    {
        int len = s.length();
        int num = 0;
        while(i<len&&s[i]!=‘.‘)//碰到结束或者‘.‘
        {
            num = num*10+(s[i]-‘0‘);
            i++;
        }
        return num;
    }
};
时间: 2024-07-29 08:49:23

【一天一道LeetCode】#165. Compare Version Numbers.md的相关文章

leetCode 165. Compare Version Numbers 字符串

165. Compare Version Numbers Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and t

[LeetCode] 165. Compare Version Numbers 比较版本数

Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

leetcode 165. Compare Version Numbers

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . charac

Java for LeetCode 165 Compare Version Numbers

Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

leetcode 165 Compare Version Numbers python

纯属吐槽..那坑爹的题目,不过也有可能是我e文没看太仔细吧 原题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits

LeetCode 165 Compare Version Numbers(比较版本号)(string)(*)

翻译 比较两个版本号version1和version2. 如果version1大于version2返回1,如果version1小于version2返回-1,否则返回0. 你可以假设版本号字符串是非空并且只包含数字和"."字符. "."字符不代表十进制中的点,而被用作分隔数字序列. 例如,2.5不是"两个半",也不是"差一半到三",而是第二版中的第五个小版本. 这里有一个版本号排序的示例: 0.1 < 1.1 < 1

leetcode——165 Compare Version Numbers(数字版本的比较)

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . charac

LeetCode:Compare Version Numbers - 比较版本号

1.题目名称 Compare Version Numbers(比较版本号) 2.题目地址 https://leetcode.com/problems/compare-version-numbers/ 3.题目内容 英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. 中

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\.&qu