当前位置:网站首页>Comparison version number [leetcode]
Comparison version number [leetcode]
2022-06-26 07:52:00 【Don't hit me, it hurts!】
Compare version number
describe : Here are two version numbers version1 and version2 , Please compare them .
The version number consists of one or more revision numbers , Each revision number consists of a ‘.’ Connect . Each revision number consists of Multiple digits form , May contain Leading zero . Each version number contains at least one character . 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 . for example ,2.5.33 and 0.1 All valid version numbers .
When comparing version numbers , Please compare their revision numbers from left to right . When comparing revision numbers , Just compare Ignore any integer values after leading zeros . in other words , Revision number 1 And revision number 001 equal . 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 , edition 1.0 Less than version 1.1 , Because they are subscript 0 The revision number of is the same , And the subscript is 1 The revision numbers of are respectively 0 and 1 ,0 < 1 .
The return rule is as follows :
- If version1 > version2 return 1,
- If version1 < version2 return -1,
- In addition, return to 0.
Tips :
- 1 <= version1.length, version2.length <= 500
- version1 and version2 Contains only numbers and ‘.’
- version1 and version2 All are Valid version number
- version1 and version2 All revision numbers of can be stored in 32 An integer in
Answer key
Ideas :
- 1. adopt . Split the string , Get array
- 2. Take the maximum length of both , As array length , Use of insufficient length 0 completion
- 3. Compare elements at the same position in the array
var compareVersion = function(version1, version2) {
// 1. adopt . Split the string
let v1 = version1.split('.');
let v2 = version2.split('.');
// 2. Take the maximum length of both , As array length
let len = (v1.length>v2.length ? v1.length:v2.length);
for(let i=0;i<len;++i){
// Use of insufficient length 0 completion
let num1 = v1[i]?parseInt(v1[i]):0;
let num2 = v2[i]?parseInt(v2[i]):0;
// 3. Compare elements at the same position in the array
if(num1>num2) return 1;
else if(num1<num2) return -1;
}
// All positions are relatively complete , Show the same
return 0
};
The article links : Compare version number 【leetcode】
* Please quote from QW’s Blog!*
边栏推荐
- Install the local Google plug-in and watch the free video through the oil monkey script
- Solve psycopg2 NotSupportedError: PQconninfo not available in libpq < 9.3
- 项目管理学习
- [UVM practice] Chapter 2: a simple UVM verification platform (4) the ultimate masterpiece of UVM: sequence
- Yyds dry inventory Druid connection pool usage
- C#/. Net phase VI 01C Foundation_ 02:vs2019 basic operations, excluding code files, smart tips, data types, differences between float and double, and differences between string and string
- What is the difference between bone conduction earphones and ordinary earphones? Advantages of bone conduction earphones
- Go language custom DNS resolver practice yyds dry inventory
- The first multi platform webcast of 2021ccf award ceremony pays tribute to the winners! CCF outstanding engineer
- How to design API return codes (error codes)?
猜你喜欢
Basic use of swiperefreshlayout, local refresh of flutterprovider
Jemter 压力测试 -基础请求-【教学篇】
Opencv鼠标事件+界面交互之绘制矩形多边形选取感兴趣区域ROI
C#/. Net phase VI 01C Foundation_ 02:vs2019 basic operations, excluding code files, smart tips, data types, differences between float and double, and differences between string and string
[UVM basics] TLM common data receiving and sending and data receiving examples
Redis (5) -- Talking about compressed list
Chapter 4 (functions and preprocessing)
JS Date object
Chapter VI (pointer)
JMeter stress test web agent local interface test [teaching]
随机推荐
1010. song backtracking with total duration divisible by 60
[NLP] vector retrieval model landing: Bottleneck and solution!
Redis (4) -- Talking about integer set
花指令wp
Children play games (greed, prefix and) - Niuke winter vacation training camp
Arrangement and insertion structure
How MySQL implements the RC transaction isolation level
指南针炒股软件开户是合法的吗?安全吗
手机开户哪个证券公司佣金最低?网上开户是否安全么?
解决 psycopg2.NotSupportedError: PQconninfo not available in libpq < 9.3
Uniapp scrolling load (one page, multiple lists)
ReW_p
Summary of domestic database examination data (continuously updated)
Here is the command to display the disk space usage. Go ahead and pay attention to more wonderful things!
The long path of Xiao Sha (graph theory, Euler diagram)
Listview control
Gavin teacher's insight on transformer live class - multi state transition of financial BOT and rasa interactive behavior analysis of Rasa project (52)
Is it legal to open an account for compass stock trading software? Is it safe?
Flutter (III) - master the usage of dart language in an article
Which securities company has the lowest Commission for opening a mobile account? Is it safe to open an account online?