Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 823 Bytes

165.-compare-version-numbers.md

File metadata and controls

31 lines (26 loc) · 823 Bytes

165. Compare Version Numbers

class Solution:
    def compareVersion(self, version1: str, version2: str) -> int:
		## preprocessing the data into list
        s1 = version1.split('.')
        s2 = version2.split('.')
        
		## Padding [0] and keep both of them with same length
        max_len = max(len(s1),len(s2))
        s1 = self.zeroPadding(s1,max_len)
        s2 = self.zeroPadding(s2,max_len)
        i = 0
		
		## Compare version
        while i < len(s1) and i<len(s2):
            if int(s1[i])== int(s2[i]):
                i += 1
            elif int(s1[i]) > int(s2[i]):
                return 1
            else:
                return -1
        return 0

    def zeroPadding(self,s,max_len):
        if len(s) < max_len:
            return s + [0] * (max_len -len(s))
        return s