Compare Version Numbers
Compare two version numbers version1 and version1.
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
本题特殊情况要考虑到。v1 和v1.0 的比较。
public class Solution {
public int compareVersion(String version1, String version2) {
String[] splits1=version1.split("\\.",-1);
String[] splits2=version2.split("\\.",-1);
int shortLength=Math.min(splits1.length,splits2.length);
int longLength=Math.max(splits2.length,splits1.length);
int i=0;
for(i=0;i<shortLength;i++)
{
if(Integer.parseInt(splits1[i])<Integer.parseInt(splits2[i]))
{
return -1;
}
else if(Integer.parseInt(splits1[i])>Integer.parseInt(splits2[i]))
{
return 1;
}
}
if(i==longLength)
{
return 0;
}
else
{
while(i<longLength)//如果两个版本号不同长度,但是是同一版本的情况,如v1.0 和v1
{
if(splits1.length==longLength)
{
if(Integer.parseInt(splits1[i])!=0)
{
return 1;
}
}
else if(splits2.length==longLength)
{
if(Integer.parseInt(splits2[i])!=0)
{
return -1;
}
}
i++;
}
return 0;
}
}
}
Compare Version Numbers的更多相关文章
- 2016.5.21——Compare Version Numbers
Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A' ) 2.srt.at(),substr ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【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 ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- [LeetCode] 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自带的工具生成证书
一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...
- sqlalchemy默认时间
我查到的sqlalchemy默认时间有2种: from sqlalchemy.sql import func time_created = Column(DateTime(timezone=True) ...
- GridView 动态添加绑定列和模板列
动态添加绑定列很简单:例如: GridView1.DataSourceID = "SqlDataSource1"; BoundField bf1 = new BoundField( ...
- Another app is currently holding the yum lock
摘要 在使用yum安装的时候,出现该error. 错误 Another app is currently holding the yum lock; waiting for it to exit... ...
- jquery 用attr修改src 淡入淡出
$("#wanwan").animate({ opacity: 'toggle' }, "slow", null, function () { $(" ...
- C#高级编程笔记 Delegate 的粗浅理解 2016年9月 13日
Delegate [重中之重] 委托 定义一:(参考)http://www.cnblogs.com/zhangchenliang/archive/2012/09/19/2694430.html 完全可 ...
- word20161215
name / 名称 name mapping / 名称映射 name resolution / 名称解析 name server (NS) resource record / 名称服务器资源记录 na ...
- dos 下删除文件、文件夹
删除文件 /p 删除每一个文件之前提示确认/f 强制删除只读文件 /s 从当前目录及所有子目录删除指定文件/q 安静模式.删除全局通配符时,不要求确认/a 根据属性选择要删除的文件 指定下列文件属性中 ...
- ubuntu15.10安装搜狗拼音输入法
sudo vim /etc/apt/sources.list.d/ubuntukylin.list(我的默认显示没有这个文件将自动创建) 添加源deb http://archive.ubuntukyl ...
- mongodb的查询语句学习摘要
看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...