Compare Version Numbers
Question
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.
Example:
Here is an example of version numbers ordering:
Answer
solution:
Knowledge:
这道题目一开始没觉得难,因为以为两者只要比较到一个没有位了,通过多次比较就可以了,也想到了用split来转成list处理。但是没想到会有"1.0"这样的问题。于是翻看了答案,就发现原来思想是补齐0到相同长度。这个比较类型题目的一个思想要学会,缩放到同样比例进行比较。
循环中,没有使用到变量值,那么就是用"_"。it's conventional in python to use _ for throwaway variables. It just indicates that the loop variable isn't actually used.
Last updated
Was this helpful?