当前位置:网站首页>165. 比较版本号
165. 比较版本号
2022-07-01 03:23:00 【Sun_Sky_Sea】
165. 比较版本号
原始题目链接:https://leetcode.cn/problems/compare-version-numbers/
给你两个版本号 version1 和 version2 ,请你比较它们。
版本号由一个或多个修订号组成,各修订号由一个 ‘.’ 连接。每个修订号由 多位数字 组成,可能包含 前导零 。每个版本号至少包含一个字符。修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,2.5.33 和 0.1 都是有效的版本号。
比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 忽略任何前导零后的整数值 。也就是说,修订号 1 和修订号 001 相等 。如果版本号没有指定某个下标处的修订号,则该修订号视为 0 。例如,版本 1.0 小于版本 1.1 ,因为它们下标为 0 的修订号相同,而下标为 1 的修订号分别为 0 和 1 ,0 < 1 。
返回规则如下:
如果 version1 > version2 返回 1,
如果 version1 < version2 返回 -1,
除此之外返回 0。
示例 1:
输入:version1 = “1.01”, version2 = “1.001”
输出:0
解释:忽略前导零,“01” 和 “001” 都表示相同的整数 “1”
示例 2:
输入:version1 = “1.0”, version2 = “1.0.0”
输出:0
解释:version1 没有指定下标为 2 的修订号,即视为 “0”
示例 3:
输入:version1 = “0.1”, version2 = “1.1”
输出:-1
解释:version1 中下标为 0 的修订号是 “0”,version2 中下标为 0 的修订号是 “1” 。0 < 1,所以 version1 < version2
提示:
1 <= version1.length, version2.length <= 500
version1 和 version2 仅包含数字和 ‘.’
version1 和 version2 都是 有效版本号
version1 和 version2 的所有修订号都可以存储在 32 位整数 中
解题思路:
字符串每个位置进行比对
代码实现:
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):
# 解析后比对两个版本号对应位置的值
x, y = int(z1), int(z2)
if x != y:
return 1 if x > y else -1
return 0
参考文献:
https://leetcode.cn/problems/compare-version-numbers/solution/bi-jiao-ban-ben-hao-by-leetcode-solution-k6wi/
边栏推荐
- [nine day training] content III of the problem solution of leetcode question brushing Report
- Promise中finally的用法
- jeecgboot输出日志,@Slf4j的使用方法
- Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
- pytorch nn. AdaptiveAvgPool2d(1)
- Filter
- Listener listener
- Introduction to EtherCAT
- Ouc2021 autumn - Software Engineering - end of term (recall version)
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
猜你喜欢
![[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

Research on target recognition and tracking based on 3D laser point cloud
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

Cookie&Session
![[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation

Explain spark operation mode in detail (local+standalone+yarn)

ASGNet论文和代码解读2

Leetcode 128 longest continuous sequence (hash set)
![Online public network security case nanny level tutorial [reaching out for Party welfare]](/img/66/d9c848a7888e547b7cb28d84aabc24.png)
Online public network security case nanny level tutorial [reaching out for Party welfare]

LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
随机推荐
Listener listener
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
Test function in pychram
E15 solution for cx5120 controlling Huichuan is620n servo error
multiple linear regression
[daily training] 1175 Prime permutation
Leetcode:829. 连续整数求和
家居网购项目
Gorilla/mux framework (RK boot): RPC error code design
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
在线公网安备案保姆级教程【伸手党福利】
TEC: Knowledge Graph Embedding with Triple Context
torch.histc
[nine day training] content III of the problem solution of leetcode question brushing Report
详解Spark运行模式(local+standalone+yarn)
ECMAScript 6.0
md5sum操作
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
4. [WebGIS practice] software operation chapter - data import and processing