Compare numbers version1 and Version2.
If Version1 > Version2 return 1, if Version1 < Version2 return-1, otherwise return 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, 2.5 are not "both and a half" or "half to version three", it is the fifth Second-level revision of the SEC Ond first-level revision.
Here are an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
This question is mainly about unequal lengths.
My practice is to add 0 to the front when the time is not equal.
Class solution: # @param version1, a string # @param version2, a string # @return An integer def compareve Rsion (self, Version1, version2): num1=version1.split ('. ') Num2=version2.split ('. ') While Len (NUM1) or Len (num2): If Len (NUM1) ==0: num1=[0] elif len (num2) ==0: num2=[0] else: I1=int (num1[0]) i2=int (num2[0]) if i1<i2: return-1 elif i1>i2: return 1 Else: num1=num1[1:] num2=num2[1:] return 0
165. Compare Version Numbers leetcode Python