165. Compare Version Numbers (String)
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
注意:版本格式可能会有多个子版本
class Solution {
public:
int compareVersion(string version1, string version2) {
vector<int> arr_v1,arr_v2;
int pos1_pre = , pos2_pre = , pos1=version1.find_first_of('.'),pos2=version2.find_first_of('.');
while(pos1!=-){
arr_v1.push_back(atoi(version1.substr(pos1_pre,pos1).c_str()));
pos1_pre = pos1+;
pos1 = version1.find_first_of('.',pos1_pre);
}
arr_v1.push_back(atoi(version1.substr(pos1_pre).c_str())); while(pos2!=-){
arr_v2.push_back(atoi(version2.substr(pos2_pre,pos2).c_str()));
pos2_pre = pos2+;
pos2 = version2.find_first_of('.',pos2_pre);
}
arr_v2.push_back(atoi(version2.substr(pos2_pre).c_str())); int size1 = arr_v1.size();
int size2 = arr_v2.size();
int size = min(size1,size2);
for(int i = ; i < size; i++){
if(arr_v1[i] > arr_v2[i]) return ;
else if(arr_v1[i] < arr_v2[i]) return -;
}
if(size == size1){
for(int i = size; i < size2; i++){
if(arr_v2[i]!=) return -;
}
return ;
}
else{
for(int i = size; i < size1; i++){
if(arr_v1[i]!=) return ;
}
return ;
}
}
};
165. Compare Version Numbers (String)的更多相关文章
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【刷题-LeetCode】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- ✡ 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 ...
- 【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 ...
- 165. Compare Version Numbers
题目: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- JAVA Spring Cloud 注册中心 Eureka 相关配置
转载至 https://www.cnblogs.com/fangfuhai/p/7070325.html Eureka客户端配置 1.RegistryFetchIntervalSecon ...
- oracle PL/SQL的介绍
转自:http://blog.sina.com.cn/s/blog_4c302f060101i4o1.html 一 PL/SQL的介绍 1 PL/SQL是什么? PL/SQL(procedural l ...
- unrecognized import path "golang.org/x/net/html"
go run的时候报:unrecognized import path "golang.org/x/net/html" 应该是被墙掉了,自己去github上下载包即可 git cl ...
- sql server导入excel数据
1.sql导入工具,数据源选择 microsoft excel 2.有odbc配置过的连接excel或其他数据库,都可以选择netframwork data provider for odbc,在ds ...
- URL分发(URLConf)
如果项目中应用太多,都写到顶层的urls.py中,如果个别应用url出问题的话,其他的应用也会受影响,所以我们需要对每个应用下面都写一个urls.py,实现分发 顶层urls.py中写:(属于blog ...
- Netty - 2
参考:Scalable IO in Java - http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf mainReactor负责处理客户端的连接请求,并将acc ...
- python语言中的数据类型
一.内存管理 1.python解释器的垃圾回收机制 垃圾:当一个值上没有人绑定任何变量名时(当引用计数为0),该值就是一个垃圾. python解释器运行时会检测值的引用计数,当引用计数=0该值会被清除 ...
- debian下redis2.8.17安装过程
下载redis源码包,我下载的是redis2.8.17 解压缩该源码包 tar zxf redis-2.8.17.tar.gz 进入解压缩后的目录 cd redis-2.8.17/ 添加redis用户 ...
- [ SHELL编程 ] shell编程中数值计算方法实例
SHELL编程中经常会涉及到数值计算,有时候对于这些计算命令使用场景容易忘记或者混淆,这里针对常用的命令做个总结.主要包括let.bc.expr.(())等. 1.let 使用格式:let 表达式,表 ...
- linux console 显示颜色【转】
http://blog.csdn.net/hejinjing_tom_com/article/details/12162491 引言: 由于在c代码中看到过打印彩色字, 又对PS1 想进一步了解,才有 ...