leetcode 165 Compare Version Numbers python

纯属吐槽。。那坑爹的题目,不过也有可能是我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大法好。。。

时间: 2024-10-07 11:28:22

leetcode 165 Compare Version Numbers python的相关文章

leetCode 165. Compare Version Numbers 字符串

165. Compare Version Numbers 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 t

[LeetCode] 165. Compare Version Numbers 比较版本数

Compare two version numbers version1 and version1.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 . characte

leetcode 165. Compare Version Numbers

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 . charac

Java for LeetCode 165 Compare Version Numbers

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 . characte

Leetcode OJ : Compare Version Numbers Python solution

Total Accepted: 12400 Total Submissions: 83230 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 contai

LeetCode 165 Compare Version Numbers(比较版本号)(string)(*)

翻译 比较两个版本号version1和version2. 如果version1大于version2返回1,如果version1小于version2返回-1,否则返回0. 你可以假设版本号字符串是非空并且只包含数字和"."字符. "."字符不代表十进制中的点,而被用作分隔数字序列. 例如,2.5不是"两个半",也不是"差一半到三",而是第二版中的第五个小版本. 这里有一个版本号排序的示例: 0.1 < 1.1 < 1

leetcode——165 Compare Version Numbers(数字版本的比较)

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 . charac

LeetCode:Compare Version Numbers - 比较版本号

1.题目名称 Compare Version Numbers(比较版本号) 2.题目地址 https://leetcode.com/problems/compare-version-numbers/ 3.题目内容 英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. 中

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\.&qu