当前位置:网站首页>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/
边栏推荐
- Feign remote call and getaway gateway
- torch. histc
- Leetcode 128 longest continuous sequence (hash set)
- How keil displays Chinese annotations (simple with pictures)
- 网页不能右键 F12 查看源代码解决方案
- 数据库DDL(Data Definition Language,数据定义语言)知识点
- 【快捷键】
- Go tool cli for command line implementation
- Server rendering technology JSP
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
猜你喜欢

Sort linked list (merge sort)

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

Home online shopping project

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

不用加减乘除实现加法

How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars

BluePrism注册下载并安装-RPA第一章

Pytorch training deep learning network settings CUDA specified GPU visible
随机推荐
torch.histc
How keil displays Chinese annotations (simple with pictures)
谷粒学院微信扫码登录过程记录以及bug解决
MFC窗口滚动条用法
SEM of C language_ Tvariable type
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
【TA-霜狼_may-《百人计划》】1.3纹理的秘密
【TA-霜狼_may-《百人计划》】2.1 色彩空间
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
Pyramid scene parsing network [pspnet] thesis reading
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
torch. histc
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
Appium自动化测试基础 — APPium基本原理
BluePrism注册下载并安装-RPA第一章
Review column - message queue
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
Include() of array
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
Data exchange JSON