Compare version numbers
Title description: You are given two version numbers version1 and version2, please compare them.
A version number consists of one or more revision numbers, each connected by a '.'. Each revision number consists of multiple digits, possibly including leading zeros. Each version number contains at least one character. Revision numbers are numbered from left to right, with subscripts starting at 0, with the leftmost revision numbered at 0, the next revision at 1, and so on. For example, both 2.5.33 and 0.1 are valid version numbers.
When comparing version numbers, compare their revision numbers in order from left to right. When comparing revision numbers, just compare the integer values ignoring any leading zeros. That is, revision number 1 and revision number 001 are equal. If the version number does not specify a revision number at a subscript, the revision number is treated as 0. For example, version 1.0 is less than version 1.1 because they have the same revision number with subscript 0, while revision numbers with subscript 1 are 0 and 1, respectively, 0 < 1 .
The return rules are as follows:
- Returns 1 if version1 > version2,
- If version1 < version2 returns -1,
- Otherwise it returns 0.
For example descriptions, please refer to the official website of LeetCode.
Source: LeetCode
Link: https://leetcode-cn.com/problems/compare-version-numbers/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Solution 1: Array Traversal
The judgment process is as follows:
- First, divide the two version numbers into version arrays according to
.
;- Then get the smaller array length count, traverse the value of the first count array, and determine which version is larger and return the corresponding value;
- If the versions of the previous count bits are the same, then judge whether the bits after the count are not 0, and if so, return the corresponding value;
- Returns 0 if the previous version numbers are the same.
public class LeetCode_165 {
public static int compareVersion(String version1, String version2) {
// 将两个版本号按照 . 分割成数组
String[] versions1 = version1.split("\\.");
String[] versions2 = version2.split("\\.");
// 获取较小的数组长度
int count = versions1.length > versions2.length ? versions2.length : versions1.length;
// 然后遍历前count位数组的值,判断哪个版本大返回相应的值
for (int i = 0; i < count; i++) {
if (Integer.valueOf(versions1[i]) > Integer.valueOf(versions2[i])) {
return 1;
} else if (Integer.valueOf(versions1[i]) < Integer.valueOf(versions2[i])) {
return -1;
}
}
// 如果前面count位的版本都一样,则判断count后面的位是否有不为0的
if (versions1.length > count) {
for (int i = count; i < versions1.length; i++) {
if (Integer.valueOf(versions1[i]) > 0) {
return 1;
}
}
}
if (versions2.length > count) {
for (int i = count; i < versions2.length; i++) {
if (Integer.valueOf(versions2[i]) > 0) {
return -1;
}
}
}
// 最后,说明版本一致,返回0
return 0;
}
public static void main(String[] args) {
String version1 = "1.01", version2 = "1.001";
// 测试用例,期望输出: 0
System.out.println(compareVersion(version1, version2));
}
}
【Daily Message】 Young is a fast train that derails, knowing that he will hit him head-on, or he will drive at full speed. Therefore, it is best to choose what you want to do when you are young, and work hard to complete it, so as to experience the joy of success.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。