题目

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. spring boot 事务

    spring事务:默认自动提交只读:@Transactional(readOnly = true)读写:@Transactional(),因为等同于@Transactional(readOnly = ...

  2. 部署到CentOS Net Core

    Net Core部署到CentOS 本文基于初次或再次尝试部署.Net Core应用到Linux服务器上,我尝试后自我总结的经验一个简单的Demo,尝试部署在Linux服务器上和跨服务器访问数据库. ...

  3. 牛客网Java刷题知识点之什么是异常、异常处理的原理是什么、为什么要使用异常、异常体系、运行时异常、普通异常、自定义异常、异常链

    不多说,直接上干货! 在这个世界不可能存在完美的东西,不管完美的思维有多么缜密,细心,我们都不可能考虑所有的因素,这就是所谓的智者千虑必有一失.同样的道理,计算机的世界也是不完美的,异常情况随时都会发 ...

  4. Zeppelin的入门使用系列之使用Zeppelin运行shell命令(二)

    不多说,直接上干货! 前期博客 Zeppelin的入门使用系列之创建新的Notebook(一) 接下来,我将以ml-100k数据集,示范如何使用Spark SQL进行数据分析与数据可视化 因为 [ha ...

  5. spring的2种类型转换器

    spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...

  6. nuxt实践

    利用手脚架搭起来的服务端渲染实例目录结构.nuxtassets 未编译的静态资源如 LESS.SASS 或 JavaScriptcomponents 用于组织应用的 Vue.js 组件middlewa ...

  7. nodejs 实践:express 最佳实践(五) connect解析

    nodejs 实践:express 最佳实践(五) connect解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需 ...

  8. ubuntu下irobot串口通讯

    在window下以前就`有一个现成的串口代码.想移植到ubuntu下,发现都不一样了.要重新找个. 折腾了一上午之后,发现自己写这个串口通讯还是有一点难度. 于是,用了github上 Erick Co ...

  9. HTTP 错误 500.0 - Internal Server Error

    最近在二次开发一个APS.NET网站,将网站部署到IIS后,输入:http://localhost/upload/ 时,报错“HTTP 错误 500.0 - Internal Server Error ...

  10. opencv c++编译

    g++ image2png.cpp -o test `pkg-config --cflags --libs opencv`