当前位置:网站首页>165. compare version numbers
165. compare version numbers
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
165. Compare version number
Original title link :https://leetcode.cn/problems/compare-version-numbers/
Here are two version numbers version1 and version2 , Please compare them .
The version number consists of one or more revision numbers , Each revision number consists of a ‘.’ Connect . Each revision number consists of Multiple digits form , May contain Leading zero . Each version number contains at least one character . The revision number is numbered from left to right , Subscript from 0 Start , The leftmost revision number is subscript 0 , The next revision number is subscript 1 , And so on . for example ,2.5.33 and 0.1 All valid version numbers .
When comparing version numbers , Please compare their revision numbers from left to right . When comparing revision numbers , Just compare Ignore any integer values after leading zeros . in other words , Revision number 1 And revision number 001 equal . If the version number does not specify a revision number at a subscript , Then the amendment number shall be deemed to be 0 . for example , edition 1.0 Less than version 1.1 , Because they are subscript 0 The revision number of is the same , And the subscript is 1 The revision numbers of are respectively 0 and 1 ,0 < 1 .
The return rule is as follows :
If version1 > version2 return 1,
If version1 < version2 return -1,
In addition, return to 0.
Example 1:
Input :version1 = “1.01”, version2 = “1.001”
Output :0
explain : Ignore leading zeros ,“01” and “001” All represent the same integer “1”
Example 2:
Input :version1 = “1.0”, version2 = “1.0.0”
Output :0
explain :version1 The subscript is not specified as 2 Revision number of , That is to say “0”
Example 3:
Input :version1 = “0.1”, version2 = “1.1”
Output :-1
explain :version1 Subscript is 0 The revision number of is “0”,version2 Subscript is 0 The revision number of is “1” .0 < 1, therefore version1 < version2
Tips :
1 <= version1.length, version2.length <= 500
version1 and version2 Contains only numbers and ‘.’
version1 and version2 All are Valid version number
version1 and version2 All revision numbers of can be stored in 32 An integer in
Their thinking :
Compare each position of the string
Code implementation :
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
from itertools import zip_longest
for z1, z2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):
# After parsing, compare the values of the corresponding positions of the two version numbers
x, y = int(z1), int(z2)
if x != y:
return 1 if x > y else -1
return 0
reference :
https://leetcode.cn/problems/compare-version-numbers/solution/bi-jiao-ban-ben-hao-by-leetcode-solution-k6wi/
边栏推荐
- Ouc2021 autumn - Software Engineering - end of term (recall version)
- 在 C 中声明函数之前调用函数会发生什么?
- What happens when a function is called before it is declared in C?
- torch. histc
- Md5sum operation
- 网页不能右键 F12 查看源代码解决方案
- TEC: Knowledge Graph Embedding with Triple Context
- You cannot right-click F12 to view the source code solution on the web page
- Asgnet paper and code interpretation 2
- AfxMessageBox和MessageBox的用法
猜你喜欢
随机推荐
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Explain spark operation mode in detail (local+standalone+yarn)
208. 实现 Trie (前缀树)
ECMAScript 6.0
[daily training] 1175 Prime permutation
166. 分数到小数
Cygwin的下载和安装配置
168. Excel表列名称
Feature Pyramid Networks for Object Detection论文理解
Server rendering technology JSP
392. 判断子序列
Database DDL (data definition language) knowledge points
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
5. [WebGIS practice] software operation - service release and permission management
Finally in promise
SEM of C language_ Tvariable type
Take you through a circuit board, from design to production (dry goods)
【快捷键】
Ultimate dolls 2.0 | encapsulation of cloud native delivery









