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的更多相关文章

  1. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  2. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  3. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  5. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  6. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  7. 【leetcode】Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...

  8. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  9. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. 如何将Emmet安装到到 Sublime text 3?

    看清楚哦~~这是Sublime text 3不是2的版本,两者的安装还是有区别的,下面的方法是我感觉比较简单的,其他的要命令什么的感觉太复杂了,经测试是OK的. ONE:建议到官方下载Sublime  ...

  2. SessionState

    SqlServer方式:1.创建数据库的方法:C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql -ssadd -sstype ...

  3. 认识VTK工作原理

    VTk通过数据流实现变信息为图形数据的. 数据流一般为:source-filter--mapper--actor--render--renderwindow--interactor. 要理解工作原理, ...

  4. CSS高效开发实战:CSS 3、LESS、SASS、Bootstrap、Foundation --读书笔记(4)构造尺寸更灵活的背景

    相比传统的图片背景来说,使用CSS构造背景色不仅可以降低网络传输的开销,更由于其尺寸的可控性受到开发者的青睐. 如设计师设计了一张背景图片作为标题背景,如图5.18所示.对于用电脑浏览网页的用户来说, ...

  5. 进阶系列五【绝对干货】----Git教程

    一.介绍 1.1Git是什么? Git是目前世界上最先进的分布式版本控制系统.什么是版本控制系统?请自行百度. 1.2Git与SVN对比有什么特点? SVN是集中式版本控制系统.版本库是集中放在中央服 ...

  6. python 环境配置

    每个项目都应该有自己的虚拟环境,如何方便的操作呢? 1. 安装 virtualenv 2. 安装 virtualenvwrapper 3. 创建目录用来存放虚拟环境 mkdir $HOME/.virt ...

  7. time和datetime时间戳---python

    time模块 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式:        1.时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 2.以数 ...

  8. 《linux命令》ps -aux详细解释

    本文转载自http://blog.chinaunix.net/uid-21516619-id-1824945.html 显示其他用户启动的进程(a) 查看系统中属于自己的进程(x) 启动这个进程的用户 ...

  9. Memcache 内存分配策略和性能(使用)状态检查

    前言: 一直在使用Memcache,但是对其内部的问题,如它内存是怎么样被使用的,使用一段时间后想看看一些状态怎么样?一直都不清楚,查了又忘记,现在整理出该篇文章,方便自己查阅.本文不涉及安装.操作. ...

  10. Excel 点滴积累

    1.Excel中截取邮件@之后的字符 MID(text, start_num, num_chars) FIND(find_text,within_text,start_num) Right(strin ...