码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] Compare Version Numbers

时间:2014-12-16 19:16:58      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:leetcode   算法   

问题描述:

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

基本思想:

从高位到低位递归解决。 注意特殊case (1,1.0);


代码:

    public int compareVersion(String version1, String version2) { //java
        if(version1.equals(version2))
            return 0;
            
        int fversion1 , fversion2;
        String sversion1,sversion2;
        if(version1.contains(".")){
            int pos = version1.indexOf(".");
            fversion1 = Integer.valueOf(version1.substring(0,pos));
            sversion1 = version1.substring(pos+1,version1.length());
        }
        else {
            fversion1 = Integer.valueOf(version1);
            sversion1 = "0";
        }
        
        if(version2.contains(".")){
            int pos = version2.indexOf(".");
            fversion2 = Integer.valueOf(version2.substring(0,pos));
            sversion2 = version2.substring(pos+1,version2.length());
        }
        else {
            fversion2 = Integer.valueOf(version2);
            sversion2 = "0";
        }
        
        if(fversion1 > fversion2)
            return 1;
        else if(fversion1 < fversion2)
            return -1;
        else return compareVersion(sversion1, sversion2);
    }


[leetcode] Compare Version Numbers

标签:leetcode   算法   

原文地址:http://blog.csdn.net/chenlei0630/article/details/41963359

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!