当前位置:网站首页>比较版本号
比较版本号
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;
}
};
边栏推荐
- 瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
- [overview of AUTOSAR four BSW]
- The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
- Illustrated network: what is virtual router redundancy protocol VRRP?
- Win10 can't be installed in many ways Problems with NET3.5
- File operation io-part2
- 基于ARM RK3568的红外热成像体温检测系统
- [introduction to AUTOSAR seven tool chain]
- Foundations of data science is free to download
- Use Jenkins II job
猜你喜欢
1.12 - 指令
Advanced pointer (I)
Basic use of sringcloud & use of component Nacos
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
How SQLSEVER removes data with duplicate IDS
Arduino开发之按键检测与正弦信号输出
Vulkan-性能及精细化
Vulkan performance and refinement
Win10 多种方式解决无法安装.Net3.5的问题
Leetcode-849: maximum distance to the nearest person
随机推荐
Array common operation methods sorting (including ES6) and detailed use
Vulkan is not a "panacea"“
【AutoSAR 五 方法论】
Key detection and sinusoidal signal output developed by Arduino
Problèmes de configuration lex & yacc & Bison & Flex
Some introduction and precautions about XML
MySQL multi table joint deletion
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
【AutoSAR 六 描述文件】
Vulkan-性能及精细化
[AUTOSAR II appl overview]
First hand evaluation of Reza electronics rz/g2l development board
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
深度剖析数据在内存中的存储
Array and collection performance comparison
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
1.11 - bus