当前位置:网站首页>Compare version number
Compare version number
2022-07-03 01:02:00 【Schuyler Hu】
problem
Niuke project will have a version number when releasing the project version , such as 1.02.11,2.14.4 wait
Now here you are 2 Version number version1 and version2, Please compare their size
The version number consists of the revision number , There is a between the revision number and the revision number "." Connect .1 A revision number may consist of multiple digits , The revision number may contain a leading 0, And it's legal . for example ,1.02.11,0.1,0.2 Are legal version numbers
Each version number contains at least 1 Revision number .
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 .
Compare the rules :
One . When comparing version numbers , Please compare their revision numbers from left to right . When comparing revision numbers , Just compare integer values after ignoring any leading zeros . such as "0.1" and "0.01" The version number of is equal
Two . 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 ,“1.1" The version number of is less than "1.1.1”. because "1.1" The version number of is equivalent to "1.1.0", The first 3 The subscript of the digit revision number is 0, Less than 1
3、 ... and . version1 > version2 return 1, If version1 < version2 return -1, Or return to 0.
Ideas
utilize getline() Split the version number , Get revision number , For leading 0 Revision number of , utilize stringstream convert to int type .
Code implementation
class Solution {
public:
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * Compare version number * @param version1 string character string * @param version2 string character string * @return int integer */
int compare(string version1, string version2) {
// write code here
stringstream ss1, ss2, sstmp;
string tmp;
int v1, v2;
ss1 << version1;
ss2 << version2;
// There is a string that goes to the end first , And read another string
while (!ss1.eof() || !ss2.eof())
{
if (ss1.eof()) v1 = 0;
else
{
// With '.' For the split symbol , Read every time you enter the cycle to '.' Split string
getline(ss1, tmp, '.');
// Convert the read string to 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;
}
};
边栏推荐
- ROS2之ESP32简单速度消息测试(极限频率)
- cordova-plugin-device获取设备信息插件导致华为审核不通过
- About qbytearray storage hexadecimal and hexadecimal conversion
- FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
- [applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
- matlab 多普勒效应产生振动信号和处理
- Solve the cache problem of reactnative using WebView
- lex && yacc && bison && flex 配置的问题
- leetcode-224:基本计算器
- 瑞萨电子RZ/G2L开发板上手评测
猜你喜欢

Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)

【AutoSAR 九 C/S原理架构】

Illustrated network: what is virtual router redundancy protocol VRRP?
![[introduction to AUTOSAR seven tool chain]](/img/cf/ed0ccf39d38e0b4fc3d97d4fd58a7e.png)
[introduction to AUTOSAR seven tool chain]
![[AUTOSAR XIII NVM]](/img/38/805ab70f199e2cfad4d9dae0e2c1ff.png)
[AUTOSAR XIII NVM]
![[AUTOSAR VI description document]](/img/3d/1382acbc4054ab218485a12b7b4e6b.png)
[AUTOSAR VI description document]
![[overview of AUTOSAR four BSW]](/img/19/c2273bbedb7f8d859e5a3805ed5740.png)
[overview of AUTOSAR four BSW]

The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022

【AutoSAR 二 AppL概述】

【爱死机】《吉巴罗》被忽略的细节
随机推荐
Infrared thermography temperature detection system based on arm rk3568
文件操作IO-Part2
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
excel IF公式判断两列是否相同
合并K个已排序的链表
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
Vulkan performance and refinement
[daily training] 871 Minimum refueling times
Win10 can't be installed in many ways Problems with NET3.5
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
线程的启动与优先级
Solve the cache problem of reactnative using WebView
Leetcode-934: the shortest Bridge
全志A40i/T3如何通过SPI转CAN
这不平凡的两年,感谢我们一直在一起!
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
删除有序链表中重复的元素-II
[shutter] image component (cached_network_image network image caching plug-in)
【AutoSAR 十三 NVM】