当前位置:网站首页>比较版本号
比较版本号
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;
}
};
边栏推荐
- 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
- First hand evaluation of Reza electronics rz/g2l development board
- leetcode-871:最低加油次数
- 【AutoSAR 八 OS】
- Sentry developer contribution Guide - configure pycharm
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- How to convert Quanzhi a40i/t3 to can through SPI
- 【AutoSAR 九 C/S原理架构】
- [case sharing] let the development of education in the new era advance with "number"
- RK3568开发板评测篇(二):开发环境搭建
猜你喜欢

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

Vulkan-性能及精细化

Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud

数学建模之线性规划(含MATLAB代码)

Advanced pointer (I)

【C语言】分支和循环语句(上)

【AutoSAR 四 BSW概述】

百度智能云牵头打造智能云综合标准化平台

Thank you for being together for these extraordinary two years!

Leetcode-2280: represents the minimum number of line segments of a line graph
随机推荐
Vulkan-实践第一弹
465. 最优账单平衡 DFS 回溯
Rk3568 development board evaluation (II): development environment construction
Advanced pointer (I)
Leetcode-2280: represents the minimum number of line segments of a line graph
There is an unknown problem in inserting data into the database
Leetcode-849: maximum distance to the nearest person
数据分析思维分析犯法和业务知识——分析方法(一)
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
Problèmes de configuration lex & yacc & Bison & Flex
这不平凡的两年,感谢我们一直在一起!
[AUTOSAR + IO Architecture]
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
测试右移:线上质量监控 ELK 实战
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
[AUTOSAR XIII NVM]
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
Rust ownership (very important)