leetcode 165. Compare version numbers - java implementation

Topic category

Simulation is fine

Link to original title

Give you two version numbers version1 and version2, please compare them.

A version number consists of one or more revision numbers joined by a '.'. Each revision number consists of multiple digits and may contain 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 number having subscript 0, the next revision number having subscript 1, and so on. For example, 2.5.33 and 0.1 are valid version numbers.

When comparing version numbers, compare their revision numbers sequentially from left to right. When comparing revision numbers, simply compare integer values ​​ignoring any leading zeros. That is, revision 1 and revision 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, and revision numbers with subscript 1 are 0 and 1 respectively, 0 < 1.

The return rules are as follows:

Returns 1 if version1 > version2,
-1 if version1 < version2,
0 otherwise.

Code Example: Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Leading zeros are ignored, "01" and "001" both represent the same integer "1"
Example 2:
Input: version1 = "1.0" , version2 = "1.0.0"
Output: 0
Explanation: version1 does not specify a revision number with a subscript of 2, which is regarded as "0"
Example 3:
Input: version1 = "0.1", version2 = "1.1"
Output: -1
Explanation: The revision number with subscript 0 in version1 is "0", and the revision number with subscript 0 in version2 is "1". 0 < 1, so version1 < version2

answer

Compare s1 and s2, enumerate the version numbers of the same level, assuming that the enumeration reaches a certain level, i is the beginning of the current level of the current s1 string, x - 1 is the end; j is the current level of the current s2 string At the beginning, y - 1 is the end, so [i, x - 1] of s1 and [j, y - 1] of s2 are version numbers with the same level of two strings, which can be converted to string comparison. As for x =
iy ==j In this case, it does not exist, that is, when s1 is null, it defaults to 0 if it does not exist

class Solution {
    
    
    public int compareVersion(String s1, String s2) {
    
    
        int i = 0 , j = 0 ;
        while(i < s1.length() || j < s2.length()){
    
    
            int x = i , y = j ;
            while(x < s1.length() && s1.charAt(x) != '.') x++;
            while(y < s2.length() && s2.charAt(y) != '.') y++;
            int a = x == i ? 0 : Integer.parseInt(s1.substring(i,x));
            int b = y == j ? 0 : Integer.parseInt(s2.substring(j,y));
            if(a > b) return 1 ;
            if(a < b) return -1 ;
            i = x + 1 ;
            j = y + 1;
        }
        return 0;
    }
}

Guess you like

Origin blog.csdn.net/qq_41810415/article/details/131077866