Compare numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return-1, otherwise ret Urn 0.
Assume that the version strings is non-empty and contain only digits and the . character.
The . character does not represent a, decimal point and was used to separate number sequences.
For instance, was not "both and 2.5 a half" or "half-to-version three", it is the fifth Second-level revision of the S Econd first-level revision.
Here are an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
The strings of two version numbers are converted to numbers in dotted order, and if the lengths are not equal, 0 is appended to the short, and the last two arrays are compared by element.
classSolution:#@param version1, a string #@param version2, a string #@return An integer defcompareversion (self, S1, S2): S1+='.'S2+='.'x1, x2= [], [] whileS1orS2: forIinchRange (len (S1)):ifS1[i] = ='.': x1.append (int (s1[:i])) BreakS1= S1[i+1:] forIinchRange (len (s2)):ifS2[i] = ='.': x2.append (int (s2[:i])) BreakS2= S2[i+1:] ifLen (x1) >Len (x2): forIinchRange (len (x1)-len (x2)): X2.append (0)ifLen (x2) >Len (x1): forIinchRange (len (x2)-len (x1)): X1.append (0) forIinchRange (len (x1)):ifX1[i] >X2[i]:return1ifX2[i] >X1[i]:return-1return0
Leetcode 165 Compare Version Numbers