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.

Example 1:

Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:

Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:

Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这道题调试了好久,一直不想上网搜别人的解法,因为感觉自己可以做出来,改来改去最后终于通过了,再上网一搜,发现果然和别人的方法不同,小有成就感。我的思路是:由于两个版本号所含的小数点个数不同,有可能是1和1.1.1比较,还有可能开头有无效0,比如01和1就是相同版本,还有可能末尾无效0,比如1.0和1也是同一版本。对于没有小数点的数字,可以默认为最后一位是小数点,而版本号比较的核心思想是相同位置的数字比较,比如题目给的例子,1.2和13.37比较,我们都知道应该显示1和13比较,13比1大,所以后面的不用再比了,再比如1.1和1.2比较,前面都是1,则比较小数点后面的数字。那么算法就是每次对应取出相同位置的小数点之前所有的字符,把他们转为数字比较,若不同则可直接得到答案,若相同,再对应往下取。如果一个数字已经没有小数点了,则默认取出为0,和另一个比较,这样也解决了末尾无效0的情况。代码如下:

解法一:

class Solution {
public:
int compareVersion(string version1, string version2) {
int n1 = version1.size(), n2 = version2.size();
int i = , j = , d1 = , d2 = ;
string v1, v2;
while (i < n1 || j < n2) {
while (i < n1 && version1[i] != '.') {
v1.push_back(version1[i++]);
}
d1 = atoi(v1.c_str());
while (j < n2 && version2[j] != '.') {
v2.push_back(version2[j++]);
}
d2 = atoi(v2.c_str());
if (d1 > d2) return ;
else if (d1 < d2) return -;
v1.clear(); v2.clear();
++i; ++j;
}
return ;
}
};

当然我们也可以不使用将字符串转为整型的atoi函数,我们可以一位一位的累加,参加如下代码:

解法二:

class Solution {
public:
int compareVersion(string version1, string version2) {
int n1 = version1.size(), n2 = version2.size();
int i = , j = , d1 = , d2 = ;
while (i < n1 || j < n2) {
while (i < n1 && version1[i] != '.') {
d1 = d1 * + version1[i++] - '';
}
while (j < n2 && version2[j] != '.') {
d2 = d2 * + version2[j++] - '';
}
if (d1 > d2) return ;
else if (d1 < d2) return -;
d1 = d2 = ;
++i; ++j;
}
return ;
}
};

由于这道题我们需要将版本号以’.'分开,那么我们可以借用强大的字符串流stringstream的功能来实现分段和转为整数,使用这种方法写的代码很简洁,如下所示:

解法三:

class Solution {
public:
int compareVersion(string version1, string version2) {
istringstream v1(version1 + "."), v2(version2 + ".");
int d1 = , d2 = ;
char dot = '.';
while (v1.good() || v2.good()) {
if (v1.good()) v1 >> d1 >> dot;
if (v2.good()) v2 >> d2 >> dot;
if (d1 > d2) return ;
else if (d1 < d2) return -;
d1 = d2 = ;
}
return ;
}
};

最后我们来看一种用C语言的字符串指针来实现的方法,这个方法的关键是用到将字符串转为长整型的strtol函数,关于此函数的用法可以参见我的另一篇博客strtol 函数用法。参见代码如下:

解法四:

class Solution {
public:
int compareVersion(string version1, string version2) {
int res = ;
char *v1 = (char*)version1.c_str(), *v2 = (char*)version2.c_str();
while (res == && (*v1 != '\0' || *v2 != '\0')) {
long d1 = *v1 == '\0' ? : strtol(v1, &v1, );
long d2 = *v2 == '\0' ? : strtol(v2, &v2, );
if (d1 > d2) return ;
else if (d1 < d2) return -;
else {
if (*v1 != '\0') ++v1;
if (*v2 != '\0') ++v2;
}
}
return res;
}
};

类似题目:

First Bad Version

参考资料:

https://leetcode.com/problems/compare-version-numbers/discuss/?orderBy=most_votes

https://leetcode.com/problems/compare-version-numbers/discuss/50774/Accepted-small-Java-solution.

https://leetcode.com/problems/compare-version-numbers/discuss/50788/My-JAVA-solution-without-split

https://leetcode.com/problems/compare-version-numbers/discuss/50804/10-line-concise-solution.-(C%2B%2B)

https://leetcode.com/problems/compare-version-numbers/discuss/50767/My-2ms-easy-solution-with-CC%2B%2B

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Compare Version Numbers 版本比较的更多相关文章

  1. LeetCode Compare Version Numbers

    原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...

  2. [LeetCode] Compare Version Numbers 字符串操作

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  3. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  4. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  6. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  7. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  8. LeetCode OJ:Compare Version Numbers(比较版本字符串)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

随机推荐

  1. 用 jQuery.ajaxSetup 实现对请求和响应数据的过滤

    不知道同学们在做项目的过程中有没有相同的经历呢?在使用 ajax 的时候,需要对请求参数和响应数据进行过滤处理,比如你们觉得就让请求参数和响应信息就这么赤裸裸的在互联网里来回的穿梭,比如这样: 要知道 ...

  2. 12.JAVA之GUI编程打开与保存文件

    功能:java图形用户界面开发,练习打开保存文件 代码如下: import java.awt.FileDialog; import java.awt.Frame; import java.awt.Me ...

  3. [Spring]01_环境配置

    )在资源库界面点击Artifacts标签,然后点击libs-release-local,展开后依次点击org -> springframework -> spring.

  4. C#7.0中有哪些新特性?

    以下将是 C# 7.0 中所有计划的语言特性的描述.随着 Visual Studio “15” Preview 4 版本的发布,这些特性中的大部分将活跃起来.现在是时候来展示这些特性,你也告诉借此告诉 ...

  5. 用python实现逻辑回归

    机器学习课程的一个实验,整理出来共享. 原理很简单,优化方法是用的梯度下降.后面有测试结果. # coding=utf-8 from math import exp import matplotlib ...

  6. angularjs和ajax的结合使用 (一)

    好久没写文了.这是一篇关于easyui配合ajax使用 的文章, 顺带介绍angularjs的使用 以及让你感受到angularjs的威力.网上对于ajax 的文也是多如牛毛 .我就不直接 从那种原生 ...

  7. 使用WampServer环境,如何配置虚拟主机域名

    很多人不会配置虚拟主机,我这里简单交一下大家,分三步: 1.在 C:\Windows\System32\drivers\etc 文件夹中的文件 Hosts 文件修改代码为: 127.0.0.1 loc ...

  8. git命令查看远程分支

    使用git remote -v命令可以查看本地设置好的所有远程仓库地址. ➜ learn-vue git:(master) git remote -v origin https://github.co ...

  9. IT软件技术人员的职位路线(从程序员到技术总监) - 部门管理经验谈

    以前写过一个文(IT从业者的职业道路(从程序员到部门经理) - 项目管理系列文章),主要介绍笔者的职业发展之路,不过该文需要后续了,因为笔者现在从事的是“产品经理”一职.从笔者的导航文([置顶]博文快 ...

  10. SQL Server查询第31到40条数据

    大致分为两种情况:ID连续和ID不连续. 1.ID连续的情况: 2.ID不连续的情况: (1).两次对表查询,效率较低. ID from A) (2).外层查询没有对表A进行查询,效率提高. ID f ...