leetcode: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
分析:题意为 比较两个版本数字version1和version2,值得注意的是.并不是代表小数点,例如:2.5表示第二个一级第五个二级版本
思路:可以进行逐位比较,把字符串转化成整型。
注意考虑test case:0.1 与 0.01 结果是:0.1>0.01
代码如下:
class Solution {
public:
int compareVersion(string version1, string version2) {
int v1, v2;
int t1 = 0, t2 = 0;
while (t1 < version1.length() || t2 < version2.length()) {
v1 = 0;
while (t1 < version1.length()) {
if (version1[t1] == '.') {
++t1;
break;
}
v1 = v1 * 10 + (version1[t1] - '0');
++t1;
}
v2 = 0;
while (t2 < version2.length()) {
if (version2[t2] == '.') {
++t2;
break;
}
v2 = v2 * 10 + (version2[t2] - '0');
++t2;
}
if (v1 > v2) return 1;
if (v1 < v2) return -1;
}
return 0;
}
};
其他解法:
class Solution {
public:
int compareVersion(string version1, string version2) {
const char *p1 = version1.c_str()-1;
const char *p2 = version2.c_str()-1;
do{
int v1 = 0, v2 =0;
if (p1){
v1=atoi(p1+1);
p1 = strchr(p1+1, '.');
}
if (p2){
v2=atoi(p2+1);
p2 = strchr(p2+1, '.');
}
if (v1<v2) return -1;
if (v2<v1) return 1;
}while(p1||p2);
return 0;
}
};
或:
class Solution {
public:
int compareVersion(string version1, string version2) {
int n = version1.size(), m = version2.size();
for (int i = 0, j = 0; i < n || j < m; i++, j++) {
size_t p = 0;
int a = ((i >= n) ? 0 : stoi(version1.substr(i), &p));
i += p;
int b = ((j >= m) ? 0 : stoi(version2.substr(j), &p));
j += p;
if (a > b) return 1;
if (a < b) return -1;
}
return 0;
}
};
或:
class Solution {
public:
int compareVersion(string version1, string version2) {
istringstream iss1(version1), iss2(version2);
string token1, token2; while(!iss1.eof() || !iss2.eof()) {
getline(iss1, token1, '.'); getline(iss2, token2, '.'); if(stoi(token1) > stoi(token2)) return 1;
if(stoi(token1) < stoi(token2)) return -1; token1 = token2 = "0";
} return 0;
}
};
leetcode:Compare Version Numbers的更多相关文章
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- Leetcode OJ : Compare Version Numbers Python solution
Total Accepted: 12400 Total Submissions: 83230 Compare two version numbers version1 and version2 ...
- Leetcode 165 Compare Version Numbers
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
随机推荐
- 一个包的libevent流程
//一个发包的流程 第一个包就是客户端的心跳包,现在加了版本的包 再来看看这个发包打包过程,过程坚持,但理解费劲 void NGP::OnliveTimer()//客户端心跳,5s发一次 { Send ...
- NGUI 笔记
1.动态加载Atlas,并用NGUITools添加Sprite UIAtlas MyAtlas = Resources.Load("MyAtlas", typeof(UIAtlas ...
- PowerDesigner(二)-项目和框架矩阵(转)
项目和框架矩阵 项目是PowerDesigner 15的新概念,通过项目系统分析/设计人员可以对模型以及各类文档进行分组.项目也可以包含框架矩阵,以表格的形式体现各个模型之间的关系. 项目和框架矩阵解 ...
- Sqlite基础及其与SQLServer语法差异
1 TOP 这是一个大家经常问到的问题,例如在SQLSERVER中可以使用如下语句来取得记录集中的前十条记录: SELECT TOP 10 * FROM [index] ORDER BY indexi ...
- Codeforces Round #242 (Div. 2) C题
题目链接:http://codeforces.com/contest/424/problem/C, 想来一个小时,就是做不出,都做出来了,悲剧! 分析:我们知道交换异或的顺序不影响答案! 然后就是求t ...
- TKStudio 4.6IDE Warning: L6310W: Unable to find ARM libraries.
我也遇到了同样的问题.搞了很久,按下面的操解决了 内容转至:http://bbs.zlgmcu.com/dv_rss.asp?s=xh&boardid=43&id=23032& ...
- 【一】php 操作符
1.php单引号和双引号的区别 单引号和双引号都能表示字符串,但是单引号不能识别里面带有转义字符'/'和变量的字符串,所以需要""去表示这种字符串.或者使用<<< ...
- POJ 1027 The Same Game(模拟)
题目链接 题意 : 一个10×15的格子,有三种颜色的球,颜色相同且在同一片内的球叫做cluster(具体解释就是,两个球颜色相同且一个球可以通过上下左右到达另一个球,则这两个球属于同一个cluste ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...