当前位置:网站首页>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/
边栏推荐
- Edlines: a real time line segment detector with a false detection control
- How to achieve 0 error (s) and 0 warning (s) in keil5
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- 【伸手党福利】JSONObject转String保留空字段
- 409. 最长回文串
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- [daily training] 1175 Prime permutation
- Leetcode 1482 guess, how about this question?
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- Ridge regression and lasso regression
猜你喜欢

Detailed list of errors related to twincat3 ads of Beifu

详解Spark运行模式(local+standalone+yarn)

E15 solution for cx5120 controlling Huichuan is620n servo error

报错:Plug-ins declaring extensions or extension points must set the singleton directive to true

Home online shopping project

访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载

Data exchange JSON

Sort linked list (merge sort)

5、【WebGIS实战】软件操作篇——服务发布及权限管理

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
随机推荐
5. [WebGIS practice] software operation - service release and permission management
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Leetcode: offer 59 - I. maximum value of sliding window
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
Pyramid Scene Parsing Network【PSPNet】论文阅读
Filter
Implement pow (x, n) function
AfxMessageBox和MessageBox的用法
Pathmeasure implements loading animation
Ultimate dolls 2.0 | encapsulation of cloud native delivery
[reach out to Party welfare] developer reload system sequence
串口接收数据方案设计
pytorch训练深度学习网络设置cuda指定的GPU可见
【伸手党福利】JSONObject转String保留空字段
IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
pytorch nn. AdaptiveAvgPool2d(1)
Gorilla/mux framework (RK boot): RPC error code design
谷粒学院微信扫码登录过程记录以及bug解决
5、【WebGIS实战】软件操作篇——服务发布及权限管理
10、Scanner. Next() cannot read spaces /indexof -1