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:

  1. 0.1 < 1.1 < 1.2 < 13.37

本题特殊情况要考虑到。v1 和v1.0 的比较。

  1. public class Solution {
  2. public int compareVersion(String version1, String version2) {
  3. String[] splits1=version1.split("\\.",-1);
  4. String[] splits2=version2.split("\\.",-1);
  5. int shortLength=Math.min(splits1.length,splits2.length);
  6. int longLength=Math.max(splits2.length,splits1.length);
  7. int i=0;
  8. for(i=0;i<shortLength;i++)
  9. {
  10. if(Integer.parseInt(splits1[i])<Integer.parseInt(splits2[i]))
  11. {
  12. return -1;
  13. }
  14. else if(Integer.parseInt(splits1[i])>Integer.parseInt(splits2[i]))
  15. {
  16. return 1;
  17. }
  18. }
  19. if(i==longLength)
  20. {
  21. return 0;
  22. }
  23. else
  24. {
  25. while(i<longLength)//如果两个版本号不同长度,但是是同一版本的情况,如v1.0 和v1
  26. {
  27. if(splits1.length==longLength)
  28. {
  29. if(Integer.parseInt(splits1[i])!=0)
  30. {
  31. return 1;
  32. }
  33. }
  34. else if(splits2.length==longLength)
  35. {
  36. if(Integer.parseInt(splits2[i])!=0)
  37. {
  38. return -1;
  39. }
  40. }
  41. i++;
  42. }
  43. return 0;
  44. }
  45. }
  46. }

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. asp.net读取execl模板并填充数据,关闭进程

    <head runat="server"> <title></title> <script src="Scripts/jquer ...

  2. JS复习--更新结束

    js复习-01---03 一 JS简介 1,文档对象模型 2,浏览器对象模型 二 在HTML中使用JS 1,在html中使用<script></script>标签 2,引入外部 ...

  3. IBatis 批量插入数据之SqlBulkCopy

    public void AddLetters(IList<int> customerIds, string title, string content, LetterEnum.Letter ...

  4. 主流ORM对比分析,莫人云亦云

    目前主流的ORM框架有Entity Framework,Dapper,NHibernate,NBear,Castle ActiveRecord,BATIS.NET六种,都是免费开源的.下边从官方支持性 ...

  5. dos 固定ip命令

    dos 固定ip命令 ***************************************************************************************** ...

  6. centos 下测试网速

    wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py chmod a+rx speedtest. ...

  7. C# 异步

    private void GetHttpResponse() { var client = new Microsoft.HBase.Client.HBaseClient(new ClusterCred ...

  8. 手动封装js原生XMLHttprequest异步请求

    Code Object.extend =function(targetObj,fnJson){ //扩展方法,类似于jQuery的$.extend,可以扩展类的方法,也可以合并对象 for(var f ...

  9. 第2月第5天 arc invocation getReturnValue

    http://blog.csdn.net/zengconggen/article/details/38024625

  10. CSS3——transform学习

    CSS动画效果可以使用transform和Animation,前者较简单,先学习前者. transform有几个基本变换,平移.旋转.缩放.扭曲 一.translate平移 有translate2d和 ...