当前位置:网站首页>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;
}
};
边栏推荐
- FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
- Leetcode 294. Flip game II (game theory)
- [case sharing] let the development of education in the new era advance with "number"
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- tail -f 、tail -F、tailf的区别
- 世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
- 正确甄别API、REST API、RESTful API和Web Service之间的异同
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- [AUTOSAR nine c/s principle Architecture]
- 递归处理组织的几种情况
猜你喜欢
随机推荐
Vulkan is not a "panacea"“
[daily training] 871 Minimum refueling times
测试右移:线上质量监控 ELK 实战
Inversion de l'intervalle spécifié dans la liste des liens
Thread start and priority
【AutoSAR 七 工具链简介】
Usage of using clause in kingbases alter table
Foundations of data science is free to download
1038 Recover the Smallest Number
删除有序链表中重复的元素-II
excel去除小数点后面的数据,将数字取整
[introduction to AUTOSAR seven tool chain]
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
What is needed to develop a domestic arm intelligent edge computing gateway
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
Infrared thermography temperature detection system based on arm rk3568
Is there a free text to speech tool to help recommend?
MySQL multi table joint deletion
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
[AUTOSAR 11 communication related mechanism]