LeetCode 165. Compare Version Numbers
2016-04-27 08:27
316 查看
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
The
For instance,
Here is an example of version numbers ordering:
I was trying to find any c++ split function.....seems like the function strtok can do the job but it is in c. But actually two pointers will work.
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.5is 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
I was trying to find any c++ split function.....seems like the function strtok can do the job but it is in c. But actually two pointers will work.
int compareVersion(string version1, string version2) { int n1 = 0, n2 = 0; int i = 0, j = 0; while(i < version1.size() || j < version2.size()) { while(i < version1.size() && version1[i] != '.') { n1 = n1*10 + (version1[i++] - '0'); } i++; while(j < version2.size() && version2[j] != '.') { n2 = n2 * 10 + (version2[j++] - '0'); } j++; if(n1 < n2) return -1; else if(n1 > n2) return 1; else {n1 = 0; n2= 0;} } return 0; }
相关文章推荐
- 【转】一份发表于2010年左右的百万级访问量网站的技术准备工作
- vtk实战(十)—文本格式转vtp格式
- hdu 1811 Rank of Tetris 拓扑排序+并查集
- Mac下Android Studio环境搭建
- UITextfield使用集合
- Git的使用记录
- 普通指针转换智能指针转换时的错误
- bzoj 3339: Rmq Problem(线段树)
- linux系统编程概述-系统调用
- cojs 疯狂的粉刷匠 疯狂的斐波那契 题解报告
- Activiti-Explorer 用户名与密码
- Activiti-Explorer 用户名与密码
- gdb 使用介绍
- Linux下使用GCC命令编译代码(1)
- [BZOJ3339]Rmq Problem(离线+线段树)
- 第k大元素
- php在原生代码中如何简单快速的发送email
- Eclipse 安装Groovy插件
- 记录一下游戏的上线
- 人生小结