码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode 165 Compare Version Numbers python

时间:2015-07-19 22:00:51      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

纯属吐槽。。那坑爹的题目,不过也有可能是我e文没看太仔细吧

原题目

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.



目测就是一个版本号比较,搞起。。。

然后就掉坑里了。。

看测试用例,尼玛。。 拿‘0‘ 跟‘1‘比较。。。 你赢了。。

继续。。 尼玛 testcase 里面有‘0.0.1‘

算你狠。。。 接着改

最后一个是‘0.1‘跟‘0.0.1‘ 比较版本...

你大爷。。。 

最终版本。。虽然确实丑了点


最终版本

# 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大法好。。。


leetcode 165 Compare Version Numbers python

标签:

原文地址:http://my.oschina.net/mummy108/blog/480508

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!