The title

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

A version number consists of one or more revision numbers connected by a ‘.’. Each revision number consists of multiple digits that may contain leading zeros. Each version number contains at least one character. Revision numbers are numbered from left to right, with subscripts 0, the leftmost revision number subscript 0, the next revision number 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 from left to right. When comparing revision numbers, only the integer values after ignoring any leading zeros are compared. That is, revision number 1 is equal to revision number 001. 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 the revision number with subscript 1 is 0 and 1, respectively, with 0 < 1.

The return rule is as follows:

Return 1 if version1 > version2, -1 if version1 < version2, and 0 otherwise.

Example 1: Input: version1 = "1.01", version2 = "1.001" Output: 0 Explanation: Ignore leading zeros, "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 subscript 2, which is regarded as "0" Example 3: Input: Version1 = "0.1", version2 = "1.1" Output: -1 Description: In version1, the revision number with subscript 0 is "0"; in version2, the revision number with subscript 0 is "1". 0 < 1, so version1 < version2 Example 4: Input: version1 = "1.0.1", version2 = "1" Output: 1 Version1 = "7.5.2.4", version2 = "7.5.3" Output: -1Copy the code

Tip:

1 <= version1.length, Version2. Length <= 500 Version1 and version2 contain only numbers and ‘.’ Both version1 and version2 are valid version numbers version1 and version2 all revision numbers can be stored in 32 An integer in

Their thinking

class Solution: def compareVersion(self, version1: str, version2: str) -> int: V1List = list(map(int,version1.split("."))) V2List = list(map(int,version2.split(".")))## to [int] For I in range(len(v1List) if len(v1List)>len(v2List) else len(v2List)): v1Val = 0 v2Val = 0 if i <= len(v1List)-1: v1Val = v1List[i] if i <= len(v2List)-1: v2Val = v2List[i] if v1Val == v2Val: continue elif v1Val > v2Val: return 1 else: return -1 return 0 if __name__ == '__main__': Version2 = "1.0.1" version1 = "1" result = Solution(). CompareVersion (version1, version2) print(result)Copy the code