【LeetCode】165. Compare Version Numbers 比较版本号(Medium)(JAVA)
题目地址: https://leetcode.com/problems/compare-version-numbers/
题目描述:
Given two version numbers, version1 and version2, compare them.
Version numbers consist of one or more revisions joined by a dot ‘.’. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.
Return the following:
- If version1 < version2, return -1.
- If version1 > version2, return 1.
- Otherwise, return 0.
Example 1:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
Example 2:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: version1 does not specify revision 2, which means it is treated as "0".
Example 3:
Input: version1 = "0.1", version2 = "1.1"
Output: -1
Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
Example 4:
Input: version1 = "1.0.1", version2 = "1"
Output: 1
Example 5:
Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1
Constraints:
- 1 <= version1.length, version2.length <= 500
- version1 and version2 only contain digits and ‘.’.
- version1 and version2 are valid version numbers.
- All the given revisions in version1 and version2 can be stored in a 32-bit integer.
题目大意
给你两个版本号 version1 和 version2 ,请你比较它们。
版本号由一个或多个修订号组成,各修订号由一个 ‘.’ 连接。每个修订号由 多位数字 组成,可能包含 前导零 。每个版本号至少包含一个字符。修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,2.5.33 和 0.1 都是有效的版本号。
比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 忽略任何前导零后的整数值 。也就是说,修订号 1 和修订号 001 相等 。如果版本号没有指定某个下标处的修订号,则该修订号视为 0 。例如,版本 1.0 小于版本 1.1 ,因为它们下标为 0 的修订号相同,而下标为 1 的修订号分别为 0 和 1 ,0 < 1 。
返回规则如下:
- 如果 version1 > version2 返回 1,
- 如果 version1 < version2 返回 -1,
- 除此之外返回 0。
解题方法
- 不断遍历,直到两个 version 都到头或者两个中间有一个节点的数字不同为止
- 需要找出 version 当前节点的版本号和下一节点的开始(便于下一次开始遍历)
class Solution {
public int compareVersion(String version1, String version2) {
int next1 = 0;
int next2 = 0;
do {
int[] temp1 = getNext(version1, next1);
int[] temp2 = getNext(version2, next2);
if (temp1[0] != temp2[0]) return temp1[0] > temp2[0] ? 1 : -1;
next1 = temp1[1];
next2 = temp2[1];
} while (next1 < version1.length() || next2 < version2.length());
return 0;
}
public int[] getNext(String version, int start) {
if (start >= version.length()) return new int[]{0, version.length()};
int res = 0;
char temp = 0;
while (start < version.length() && (temp = version.charAt(start)) != '.') {
start++;
res = res * 10 + temp - '0';
}
return new int[]{res, start + 1};
}
}
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:36.3 MB,击败了97.04% 的Java用户