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 ...
随机推荐
- 不把C作为第一门语言是个好主意么
---------------------------------------------------------------------------------- 不把C作为第一门语言是个好主意 ...
- SaaS模式的软件
SaaS是Software-as-a-Service(软件即服务)的简称,随着互联网技术的发展和应用软件的成熟, 在21世纪开始兴起的一种完全创新的软件应用模式.它与"on-demand ...
- 在Application中集成Microsoft Translator服务之获取访问令牌
我在这里画了一张图来展示业务逻辑 在我们调用microsoft translator server之前需要获得令牌,而且这个令牌的有效期为10分钟.下表列出所需的参数和对于的说明 参数 描述 clie ...
- struts2 用if标签判断字符串包含
String testStr = "用来判断是否包含的字符串"; <s:property value="testStr"/> <s:if te ...
- TCP/IP三次握手和HTTP过程
1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在"无差别&qu ...
- 双击导航栏自动滑动ListView到顶部
有些app都实现了双击导航栏让页面的list自动滑动到顶部的feature. 先实现一个继承于OnTouchListener的监听多次点击事件的监听器,通过callback把连续点击的次数返回给客户代 ...
- Linux From Scratch - Version 7.7-systemd (中文)
Linux From Scratch - Version 7.7-systemd: https://linux.cn/lfs/LFS-BOOK-7.7-systemd/index.html
- Java设计模式之创建型模式
创建型模式分为五类:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式 一.工厂方法模式:接口-实现类.工厂类
- nginx笔记资料
通配 hash 表 ngx_hash_init 实现注释:http://blog.csdn.net/gsnumen/article/details/7817396 ngx_hash_init之后的结构 ...
- go:匿名函数与闭包
一.匿名函数 定义:没有函数名的函数. 作用:在go语言中目前了解的作用就是用于构成闭包. *注:由于javascript不存在块级作用域,故匿名函数常用来包含代码以不污染全局命名空间,运行后销毁环境 ...