当前位置:网站首页>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/
边栏推荐
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- AfxMessageBox和MessageBox的用法
- BluePrism注册下载并安装-RPA第一章
- 409. 最长回文串
- 10、Scanner. Next() cannot read spaces /indexof -1
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- 网页不能右键 F12 查看源代码解决方案
- Review column - message queue
- File upload and download
- Appium自动化测试基础--补充:C/S架构和B/S架构说明
猜你喜欢

不用加减乘除实现加法

Gorilla/mux framework (RK boot): RPC error code design

Home online shopping project

pytorch训练深度学习网络设置cuda指定的GPU可见

Feign remote call and getaway gateway

Thread data sharing and security -threadlocal
![[nine day training] content III of the problem solution of leetcode question brushing Report](/img/7e/1e76181e56ef7feb083f9662df71c7.jpg)
[nine day training] content III of the problem solution of leetcode question brushing Report

idea插件备份表

复习专栏之---消息队列

后台系统右边内容如何出现滚动条和解决双滚动条的问题
随机推荐
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Develop industrial Internet with the technical advantages of small programs
Gorilla/mux framework (RK boot): RPC error code design
Unexpected token o in JSON at position 1 ,JSON解析问题
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
Learning notes for introduction to C language multithreaded programming
208. 实现 Trie (前缀树)
[daily training] 1175 Prime permutation
pytorch nn. AdaptiveAvgPool2d(1)
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Appium自动化测试基础 — APPium基本原理
Download and installation configuration of cygwin
Review column - message queue
Valentine's Day is nothing.
详解Spark运行模式(local+standalone+yarn)
bootsrap中的栅格系统
Golang multi graph generation gif
[reach out to Party welfare] developer reload system sequence
ECMAScript 6.0