当前位置:网站首页>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;
}
};
边栏推荐
- Deep analysis of data storage in memory
- [case sharing] let the development of education in the new era advance with "number"
- FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
- 【AutoSAR 十 IO架构】
- [AUTOSAR nine c/s principle Architecture]
- Initial order of pointer (basic)
- Advanced pointer (I)
- KingbaseES ALTER TABLE 中 USING 子句的用法
- 【AutoSAR 八 OS】
- Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
猜你喜欢

拥抱平台化交付的安全理念

研发一款国产ARM智能边缘计算网关需要什么

Linear programming of mathematical modeling (including Matlab code)

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities

【AutoSAR 七 工具链简介】
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

安全运营四要素之资产、脆弱性、威胁和事件
![[overview of AUTOSAR three RTE]](/img/6a/0df33beb42f165af77a17b5d8b01e2.png)
[overview of AUTOSAR three RTE]

【AutoSAR 八 OS】

【AutoSAR 六 描述文件】
随机推荐
Solve the cache problem of reactnative using WebView
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
Thread start and priority
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
KingbaseES ALTER TABLE 中 USING 子句的用法
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
Vulkan practice first bullet
leetcode-934:最短的桥
Leetcode-2280: represents the minimum number of line segments of a line graph
【AutoSAR 四 BSW概述】
Explain the basic concepts and five attributes of RDD in detail
cordova-plugin-device获取设备信息插件导致华为审核不通过
Advanced pointer (I)
1038 Recover the Smallest Number
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
File operation io-part2
How to systematically learn machine learning
Vulkan-性能及精细化
mysql 多表联合删除
Arduino开发之按键检测与正弦信号输出