leetcode 165 Compare Version Numbers python

純屬吐槽。。那坑爹的題目,不過也有多是我e文沒看太仔細吧 python

原題目 git

Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.

Here is 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.



目測就是一個版本號比較,搞起。。。

而後就掉坑裏了。。 shell

看測試用例,尼瑪。。 拿'0' 跟'1'比較。。。 你贏了。。 app

繼續。。 尼瑪 testcase 裏面有'0.0.1' 測試

算你狠。。。 接着改 this

最後一個是'0.1'跟'0.0.1' 比較版本... code

你大爺。。。  three

最終版本。。雖然確實醜了點 ci


最終版本 pycharm

# coding = utf8


class Solution:
    # @param {string} version1
    # @param {string} version2
    # @return {integer}
    def compareVersion(self, version1, version2):
        v1 = self.compareVersionToTuple(version1)
        v2 = self.compareVersionToTuple(version2)

        deep = max(len(v1), len(v2), 2)

        while len(v1) < deep:
            v1.append(0)

        while len(v2) < deep:
            v2.append(0)

        for n in range(deep):
            sv_1 = v1[n]
            sv_2 = v2[n]
            if sv_1 > sv_2:
                return 1
            elif sv_1 < sv_2:
                return -1
        return 0


    def compareVersionToTuple(self, version):
        v = []
        substrings = version.split('.')
        for str in substrings:
            v.append(int(str))
        return v

    def __init__(self):
        ans = self.compareVersion('0.1', '0.0.1')
        print(ans)


solution = Solution()



最後pycharm大法好。。。

相關文章
相關標籤/搜索