当前位置:网站首页>比较版本号
比较版本号
2022-07-03 00:29:00 【Schuyler Hu】
问题
牛客项目发布项目版本时会有版本号,比如1.02.11,2.14.4等等
现在给你2个版本号version1和version2,请你比较他们的大小
版本号是由修订号组成,修订号与修订号之间由一个"."连接。1个修订号可能有多位数字组成,修订号可能包含前导0,且是合法的。例如,1.02.11,0.1,0.2都是合法的版本号
每个版本号至少包含1个修订号。
修订号从左到右编号,下标从0开始,最左边的修订号下标为0,下一个修订号下标为1,以此类推。
比较规则:
一. 比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较忽略任何前导零后的整数值。比如"0.1"和"0.01"的版本号是相等的
二. 如果版本号没有指定某个下标处的修订号,则该修订号视为0。例如,“1.1"的版本号小于"1.1.1”。因为"1.1"的版本号相当于"1.1.0",第3位修订号的下标为0,小于1
三. version1 > version2 返回1,如果 version1 < version2 返回-1,不然返回0.
思路
利用 getline() 对版本号进行分割,得到修订号,针对有前导 0 的修订号,利用 stringstream 转换成 int 类型。
代码实现
class Solution {
public:
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */
int compare(string version1, string version2) {
// write code here
stringstream ss1, ss2, sstmp;
string tmp;
int v1, v2;
ss1 << version1;
ss2 << version2;
// 有一个字符串先到末尾,还有读取另一个字符串
while (!ss1.eof() || !ss2.eof())
{
if (ss1.eof()) v1 = 0;
else
{
// 以 '.' 为分割符号,每次进入循环读取以 '.'分割的字符串
getline(ss1, tmp, '.');
// 将读取的字符串转换成 int
sstmp << tmp;
sstmp >> v1;
sstmp.clear();
}
if (ss2.eof()) v2 = 0;
else
{
getline(ss2, tmp, '.');
sstmp << tmp;
sstmp >> v2;
sstmp.clear();
}
if (v1 > v2) return 1;
if (v1 < v2) return -1;
}
return 0;
}
};
边栏推荐
- Shell implements basic file operations (cutting, sorting, and de duplication)
- Solve the cache problem of reactnative using WebView
- RK3568开发板评测篇(二):开发环境搭建
- 【AutoSAR 五 方法论】
- [love crash] neglected details of gibaro
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- 基于ARM RK3568的红外热成像体温检测系统
- [shutter] image component (cached_network_image network image caching plug-in)
- 解决ReactNative使用webView存在缓存问题
- Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
猜你喜欢

指针初阶(基础)

Rust ownership (very important)

Shell implements basic file operations (cutting, sorting, and de duplication)

图解网络:什么是虚拟路由器冗余协议 VRRP?

Shell implements basic file operations (SED edit, awk match)
![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)
[AUTOSAR I overview]
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]

【AutoSAR 四 BSW概述】

测试右移:线上质量监控 ELK 实战

Nacos+openfeign error reporting solution
随机推荐
First hand evaluation of Reza electronics rz/g2l development board
An excellent orm in dotnet circle -- FreeSQL
Thread start and priority
1.11 - bus
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
解决ReactNative使用webView存在缓存问题
【AutoSAR 四 BSW概述】
图解网络:什么是虚拟路由器冗余协议 VRRP?
百度智能云牵头打造智能云综合标准化平台
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Illustrated network: what is virtual router redundancy protocol VRRP?
465. 最优账单平衡 DFS 回溯
详解RDD基本概念、RDD五大属性
Shell implements basic file operations (SED edit, awk match)
Hdu3507 (slope DP entry)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Win10 can't be installed in many ways Problems with NET3.5
Leetcode-1964: find the longest effective obstacle race route to each position
全志A40i/T3如何通过SPI转CAN
【JetCache】JetCache的配置说明和注解属性说明