博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
阅读量:4670 次
发布时间:2019-06-09

本文共 3138 字,大约阅读时间需要 10 分钟。

Compare two version numbers version1 and version2.

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

 

 

比较两个字符串数字的大小:(小数点可能不止一个)

注意的地方:

1、多个小数点的处理。

2、考虑清楚细节就可以了,题目本身不难。

 

 

1、直接判断,代码较多。

public class Solution {    public int compareVersion(String version1, String version2) {        int len1 = version1.length();        int len2 = version2.length();        int start1 = 0;        int start2 = 0;        int[] nums = new int[2];        while (start1 < len1 && start2 < len2){            nums = getNum(version1, start1, len1);            int num1 = nums[1];            start1 = nums[0];            nums = getNum(version2, start2, len2);            int num2 = nums[1];            start2 = nums[0];            if (num1 > num2){                return 1;            } else if (num1 < num2){                return -1;            }        }        if (start1 >= len1 && start1 >= len2){            return 0;        } else if (start1 >= len1){            while (start2 < len2){                nums = getNum(version2, start2, len2);                if (nums[1] != 0){                    return -1;                }                start2 = nums[0];            }            return 0;        } else {            while (start1 < len1){                nums = getNum(version1, start1, len1);                if (nums[1] != 0){                    return 1;                }                start1 = nums[0];            }            return 0;        }    }    public int[] getNum(String str, int start, int len){        int end = start;        int[] result = new int[2];        while (end < len && str.charAt(end) != '.'){            end++;        }        result[0] = end+1;        if( end == start){            result[1] = 0;        } else {            result[1] = Integer.valueOf(str.substring(start,end));        }        return result;    }}

 

 

2、同样的处理方式,也有很简单的写法(参考discuss)

public class Solution {    public int compareVersion(String version1, String version2) {        int temp1 = 0,temp2 = 0;        int len1 = version1.length(),len2 = version2.length();        int i = 0,j = 0;        while(i
temp2) return 1; else if(temp1

 

 

3、使用split()。(参考discuss)

public int compareVersion(String version1, String version2) {        String[] v1 = version1.split("\\.");    String[] v2 = version2.split("\\.");        for ( int i = 0; i < Math.max(v1.length, v2.length); i++ ) {        int num1 = i < v1.length ? Integer.parseInt( v1[i] ) : 0;        int num2 = i < v2.length ? Integer.parseInt( v2[i] ) : 0;        if ( num1 < num2 ) {            return -1;        } else if ( num1 > num2 ) {            return +1;        }    }         return 0;}

 

转载于:https://www.cnblogs.com/xiaoba1203/p/6114289.html

你可能感兴趣的文章
【洛谷 T47488】 D:希望 (点分治)
查看>>
spring-MVC访问静态资源
查看>>
JavaScript异步加载与同步加载
查看>>
Eclipse搭建springboot项目(六)全局异常
查看>>
Android 报错:error: too many padding sections on bottom border
查看>>
IOS开发 ios7适配
查看>>
1806. [NOIP2014]无线网路发射器选址
查看>>
根据指定的commit查找对应的log
查看>>
处理和引发事件的规范
查看>>
点击LinearLayout使用selector改变TextView字体颜色
查看>>
Markdown入门指南-指间阁
查看>>
Android Support Annotations
查看>>
JAVA设计模式之享元模式
查看>>
命令行添加删除tomcat服务
查看>>
myEclipse怎样将程序部署到tomcat(附录MyEclipse调试快捷键)
查看>>
Unity UGUI——UI基础,Canvas
查看>>
如果战斗机飞行员弹出,自动驾驶仪会接管飞机安全降落么?
查看>>
Windows Azure Azure 简介
查看>>
java字符串截取指定下标位置的字符串
查看>>
Ubuntu 14.04开启ssh服务
查看>>