Java for LeetCode 165 Compare Version Numbers

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
解题思路:

先进行split,然后转为int比较即可,JAVA实现如下:

    static public int compareVersion(String version1, String version2) {
        String[] s1=version1.split("\\.");
        String[] s2=version2.split("\\.");
        int[] num1=new int[Math.max(s1.length, s2.length)];
        int[] num2=new int[Math.max(s1.length, s2.length)];
        for(int i=0;i<s1.length;i++)
        	num1[i]= Integer.valueOf(s1[i]);
        for(int i=0;i<s2.length;i++)
        	num2[i]= Integer.valueOf(s2[i]);
        for(int i=0;i<num1.length;i++){
        	if(num1[i]>num2[i])
        		return 1;
        	else if(num1[i]<num2[i])
        		return -1;
        }
        return 0;
    }
时间: 2024-10-16 14:50:24

Java for LeetCode 165 Compare Version Numbers的相关文章

leetCode 165. Compare Version Numbers 字符串

165. Compare Version Numbers 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 t

[LeetCode] 165. 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 . characte

leetcode 165. Compare Version Numbers

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 . charac

leetcode 165 Compare Version Numbers python

纯属吐槽..那坑爹的题目,不过也有可能是我e文没看太仔细吧 原题目 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

LeetCode 165 Compare Version Numbers(比较版本号)(string)(*)

翻译 比较两个版本号version1和version2. 如果version1大于version2返回1,如果version1小于version2返回-1,否则返回0. 你可以假设版本号字符串是非空并且只包含数字和"."字符. "."字符不代表十进制中的点,而被用作分隔数字序列. 例如,2.5不是"两个半",也不是"差一半到三",而是第二版中的第五个小版本. 这里有一个版本号排序的示例: 0.1 < 1.1 < 1

leetcode——165 Compare Version Numbers(数字版本的比较)

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 . charac

LeetCode:Compare Version Numbers - 比较版本号

1.题目名称 Compare Version Numbers(比较版本号) 2.题目地址 https://leetcode.com/problems/compare-version-numbers/ 3.题目内容 英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. 中

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\.&qu

No.165 Compare Version Numbers

No.165 Compare Version NumbersCompare 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