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

Hide Tags: String

解题思路:

(1)使用splt(“\\.”)对应小数点进行分离,然后逐个比较

(2)比较过程中,我们假定数组的长度一致。长度较短的数组,当比较到最后一个元素以后,往后自动加0操作

代码如下:

	public static int Version(String version1, String version2)
	{
		//使用分隔符,然后进行比较
		String[] str1=version1.split("\\.");
		String[] str2=version2.split("\\.");
		//获取需要比例的长度为两版本中长度的大者
		int length=Math.max(str1.length, str2.length);
		for (int i = 0; i < length; i++)
		{
			/*
			 * 下面两行代码的意思将两版本的长度设置成一样
			 * 如version1=1.1 version2=1.1.2
			 * 执行完 int num1=i<str1.length?Integer.parseInt(str1[i]):0 后
			 * version1=1.1.0,这样方便比较
			 */
			int num1=i<str1.length?Integer.parseInt(str1[i]):0;
			int num2=i<str2.length?Integer.parseInt(str2[i]):0;

			if (num1>num2)
			{
				return 1;
			}
			else if (num1<num2)
			{
				return -1;
			}
		}
		return 0;
	}
时间: 2024-10-13 04:27:45

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(比较版本号)(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 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

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

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