Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- The idea is to split both version strings into lists of integers so each revision level can be compared numerically instead of lexicographically.
- We then iterate up to the maximum length of both lists, treating missing revision levels as 0, and return as soon as a difference is found; if no difference exists, the versions are equal.
- """
- class Solution:
- def compareVersion(self, version1: str, version2: str) -> int:
- v1 = list(map(int, version1.split('.')))
- v2 = list(map(int, version2.split('.')))
- n = max(len(v1), len(v2))
- for i in range(n):
- num1 = v1[i] if i < len(v1) else 0
- num2 = v2[i] if i < len(v2) else 0
- if num1 > num2:
- return 1
- elif num1 < num2:
- return -1
- return 0
Advertisement
Add Comment
Please, Sign In to add comment