当前位置:网站首页>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/
边栏推荐
- Go tool cli for command line implementation
- [reach out to Party welfare] developer reload system sequence
- Gorilla/mux framework (RK boot): RPC error code design
- Promise中finally的用法
- Appium自动化测试基础--补充:C/S架构和B/S架构说明
- Detailed explanation of ES6 deconstruction grammar
- Thread data sharing and security -threadlocal
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- Ridge regression and lasso regression
- The shell script uses two bars to receive external parameters
猜你喜欢
静态库使用MFC和共享库使用MFC的区别
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Research on target recognition and tracking based on 3D laser point cloud
过滤器 Filter
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
后台系统右边内容如何出现滚动条和解决双滚动条的问题
Unexpected token o in JSON at position 1 ,JSON解析问题
Use of comment keyword in database
How to achieve 0 error (s) and 0 warning (s) in keil5
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
随机推荐
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
Take you through a circuit board, from design to production (dry goods)
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
【伸手党福利】JSONObject转String保留空字段
pytorch训练深度学习网络设置cuda指定的GPU可见
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
串口接收数据方案设计
Leetcode 1482 guess, how about this question?
Sort linked list (merge sort)
pytorch nn. AdaptiveAvgPool2d(1)
数据交换 JSON
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
File upload and download
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
TEC: Knowledge Graph Embedding with Triple Context
Pyramid Scene Parsing Network【PSPNet】论文阅读
Jeecgboot output log, how to use @slf4j
不用加减乘除实现加法
Online public network security case nanny level tutorial [reaching out for Party welfare]
用小程序的技术优势发展产业互联网