zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }


  • 相关阅读:
    linux下设置SSH无密码登陆
    设置sudo权限
    集群重启后启动ambari-server访问Web页面无法启动集群解决
    使用Ambari部署hadoop集群
    centos7.6安装python3.7
    Locust
    测试框架(自然语言)
    Maven之(七)pom.xml配置文件详解
    git的使用
    elastic search(es)安装
  • 原文地址:https://www.cnblogs.com/eva_sj/p/6172257.html
Copyright © 2011-2022 走看看