当前位置:网站首页>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;
}
};
边栏推荐
- 安全运营四要素之资产、脆弱性、威胁和事件
- Leetcode-2115: find all the dishes that can be made from the given raw materials
- [AUTOSAR five methodology]
- Vulkan is not a "panacea"“
- Win10 多种方式解决无法安装.Net3.5的问题
- 【AutoSAR 九 C/S原理架构】
- [AUTOSAR VI description document]
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
- leetcode-871:最低加油次数
- Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
猜你喜欢
【AutoSAR 四 BSW概述】
【AutoSAR 十一 通信相关机制】
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Rk3568 development board evaluation (II): development environment construction
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Rust ownership (very important)
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
Vulkan practice first bullet
随机推荐
matlab查找某一行或者某一列在矩阵中的位置
[introduction to AUTOSAR seven tool chain]
【AutoSAR 三 RTE概述】
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
Win10 多种方式解决无法安装.Net3.5的问题
ROS2之ESP32简单速度消息测试(极限频率)
[daily training] 871 Minimum refueling times
Rust string slicing, structs, and enumeration classes
Initial order of pointer (basic)
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
[shutter] image component (cached_network_image network image caching plug-in)
Leetcode-849: maximum distance to the nearest person
excel表格计算时间日期的差值,并转化为分钟数
Usage of using clause in kingbases alter table
【AutoSAR 八 OS】
How to convert Quanzhi a40i/t3 to can through SPI
基于ARM RK3568的红外热成像体温检测系统
数学建模之线性规划(含MATLAB代码)
【AutoSAR 十三 NVM】