题目

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

分析

方法一:

采用字符串比较;此题中的版本比较,需要将.号前的部分与第二部分分开比较;

方法二:

将版本号的字符串转化为整数;

AC代码

class Solution {
public:
/*方法一:字符串比较;如果version1 > version2 则返回1 相等返回0 反之返回-1*/
int compareVersion1(string version1, string version2) {
if (version1.empty() && version2.empty())
return 0;
else if (version2.empty())
return 1;
else if (version1.empty())
return -1;
else
{
//分别得到版本号的第一部分和第二部分
int pos = 0;
for (int i = 0; version1[i] != '\0' && version1[i] != '.'; ++i)
{
++pos;
}//for
string fir_version1;
string sec_version1;
if (pos == version1.length())
{
fir_version1 = version1;
sec_version1 = "";
}
else{
fir_version1 = version1.substr(0, pos);
sec_version1 = version1.substr(pos + 1, version1.length() - pos - 1);
}
//要略过第一部分版本号的开头的0
pos = 0;
while (fir_version1[pos] != '\0' && fir_version1[pos] == '0')
{
++pos;
}//while
if (fir_version1[pos] == '\0')
fir_version1 = "0";
else
fir_version1 = fir_version1.substr(pos, fir_version1.length() - pos); pos = 0;
for (int i = 0; version2[i] != '\0' && version2[i] != '.'; ++i)
{
++pos;
}//for
string fir_version2;
string sec_version2;
if (pos == version2.length())
{
fir_version2 = version2;
sec_version2 = "";
}
else{
fir_version2 = version2.substr(0, pos);
sec_version2 = version2.substr(pos + 1, version2.length() - pos - 1);
}//else pos = 0;
while (fir_version2[pos] != '\0' && fir_version2[pos] == '0')
{
++pos;
}//while
if (fir_version2[pos] == '\0')
fir_version2 = "0";
else
fir_version2 = fir_version2.substr(pos, fir_version2.length() - pos); if (strcmp(fir_version1.c_str(), fir_version2.c_str()) != 0)
return strcmp(fir_version1.c_str(), fir_version2.c_str());
else
return strcmp(sec_version1.c_str(), sec_version2.c_str());
}
} //方法二:化为整数比较
int compareVersion(string version1, string version2) {
int val1, val2;
int idx1 = 0, idx2 = 0;
while (idx1 < version1.length() || idx2 < version2.length()) {
val1 = 0;
while (idx1 < version1.length()) {
if (version1[idx1] == '.') {
++idx1;
break;
}
val1 = val1 * 10 + (version1[idx1] - '0');
++idx1;
}
val2 = 0;
while (idx2 < version2.length()) {
if (version2[idx2] == '.') {
++idx2;
break;
}
val2 = val2 * 10 + (version2[idx2] - '0');
++idx2;
}
if (val1 > val2) return 1;
if (val1 < val2) return -1;
}
return 0;
}
};

GitHub测试程序源码

LeetCode(165) Compare Version Numbers的更多相关文章

  1. LeetCode(68)-Compare Version Numbers

    题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if versio ...

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

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

  3. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

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

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

  5. 165. Compare Version Numbers - LeetCode

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

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

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

  7. 2016.5.21——Compare Version Numbers

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

  8. 新概念英语(1-65)Not a Baby

    新概念英语(1-65)Not a Baby Does Jill take the key to the front door? A:What are you going to do this even ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. 069 Sqrt(x) 求平方根

    实现 int sqrt(int x) 函数.计算并返回 x 的平方根.x 保证是一个非负整数.案例 1:输入: 4输出: 2案例 2:输入: 8输出: 2说明: 8 的平方根是 2.82842..., ...

  2. BI的意义

    BI系统建设的价值,有可能不值钱,也有可能价值数千万,就看我们大家好用了没.”所以,BI系统建设的收获,终究还是因企业而异的,再归根,便是与企业的文化,与企业的人,尤其是管理层是极为相关的了. 商业智 ...

  3. 客户端rsyslog配置文件详解

    客户端rsyslog配置文件详解 最近再开发一个rsyslog的接收服务端,支持udp,tcp和tls三种协议.所以去仔细研究了一下rsyslog.conf的配置文件,下面来详细说一下. 因为我这儿重 ...

  4. 字符串(String)杂谈

    作者:臧圩人(zangweiren) 网址:http://zangweiren.javaeye.com >>>转载请注明出处!<<< 上一次我们已经一起回顾了面试题 ...

  5. HDU 4741 Save Labman No.004 (几何)

    题意:求空间两线的最短距离和最短线的交点 题解: 线性代数和空间几何,主要是用叉积,点积,几何. 知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形 ...

  6. 关于 NetBackup 应答文件(/tmp/NBInstallAnswer.conf)

    关于 NetBackup 应答文件 在 UNIX 和 Linux 安装和升级期间使用 NetBackup 应答文件 (/tmp/NBInstallAnswer.conf),以便: 覆盖某些默认值. 避 ...

  7. HTML5中的webSocket、ajax、http

    本文原链接:https://cloud.tencent.com/developer/article/1115496 https://cloud.tencent.com/developer/articl ...

  8. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  9. -安装与配置 FTP 服务器

    我们经常会使用 FTP,把本地电脑上的文件上传到服务器上,或者把服务器上的文件下载到自己的电脑里面.FTP 有服务端和客户端,FTP 的服务端提供了这种传输文件的服务,FTP 的客户端提供了传输文件的 ...

  10. JQuery EasyUI学习记录(二)

    1.jquery easyUI动态添加选项卡(查看jquery easyUI手册) 1.1 用于动态添加一个选项卡 1.1.1 选中指定的选项卡和判断某个选项卡是否存在 测试代码: <a id= ...