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 ...
随机推荐
- JS操作cookies方法
/** * 写入cookies */function setCookie(name, value) { var time = "1d"; //默认一天失效 var strsec = ...
- 有米实习-用到的shell脚本和Python脚本记录
Shell:LOG_DATE=`date -d "1 day ago" +%Y-%m-%d` #以指定格式设置一天前的年份月份日期 aws s3 ls $LAST5_BASE_PA ...
- Python Day9
Paramiko模块 paramiko模块基于SSH用于连接远程服务器并执行相关操作 基于用户名密码连接: import paramiko # 创建SSH对象 ssh = paramiko.SSHCl ...
- fscanf()函数基本用法
FILE *fp; while(!feof(fp)) { fscanf(fp,"%s%d%lf",a,&b,&c);//这里%s对应的a不需要加上取地址符号& ...
- webpack解惑:require的五种用法 (转)
我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...
- Debian 8安装中文字体
1.使用的镜像是debian-8.3.0-amd64-kde-CD-1.iso,下载链接可在Debian网站找到,系统安装完成后中文显示为方框 2.安装字体 apt-get install xfont ...
- 记录-div绝对定位针对手机浏览器的区别
最近搞个wap ,有一个需求就是一些文字描述定位到一张图片上的某个地方,按照以往的方式直接通过定位div position: absolute; PC chrome 模拟手机显示没问题! 但是,在ip ...
- Python之路【第二十二篇】CMDB项目
浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...
- java读取项目根路径下和任意磁盘位置下的properties文件
1.读取项目根路径下的properties文件比较简单也是比较常见的一种操作. 具体代码如下: package com.xuanen.util; import java.util.Properties ...
- Reprint: ADB is Not Recognized as an internal or external command Fix
ADB: Android Debug Bridge http://zacktutorials.blogspot.hk/2013/04/adb-is-not-recognized-as-internal ...