forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path165 Compare Version Numbers.js
More file actions
44 lines (34 loc) · 885 Bytes
/
165 Compare Version Numbers.js
File metadata and controls
44 lines (34 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Leetcode #165
// Language: Javascript
// Problem: https://leetcode.com/problems/compare-version-numbers/
// Author: Chihung Yu
/**
* @param {string} version1
* @param {string} version2
* @return {number}
*/
var comparator = function(v1,v2){
}
var compareVersion = function(version1, version2) {
var arr1 = version1.split('.');
var arr2 = version2.split('.');
var index = 0;
var len = Math.max(arr1.length, arr2.length);
while(index < len){
var v1 = parseInt(arr1[index]);
var v2 = parseInt(arr2[index]);
if(isNaN(v1) && v2 !== 0){
return -1;
}
if(isNaN(v2) && v1 !== 0){
return 1;
}
if(v1 > v2){
return 1;
} else if(v1 < v2){
return -1;
}
index++;
}
return 0;
};