Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7dfcb5c

Browse files
add: Compare Version Numbers
1 parent 7ea002f commit 7dfcb5c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4242
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js)|Medium|
4343
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/) | [JavaScript](./src/clone-graph/res.js)|Medium|
4444
|136|[Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js)|Easy|
45-
4645
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [JavaScript](./src/reverse-words-in-a-string/res.js)|Medium|
4746
|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [JavaScript](./src/maximum-product-subarray/res.js)|Medium|
47+
|165|[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) | [JavaScript](./src/compare-version-numbers/res.js)|Medium|
4848
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
4949
|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [SQL](./src/second-highest-salary/res.txt)|Easy|
5050
|177|[Nth Highest Salary](https://leetcode.com/problems/nth-highest-salary/)| [SQL](./src/nth-highest-salary/res.txt)|Medium|

‎src/compare-version-numbers/res.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017年05月11日 13:49:44
5+
*
6+
* @param {string} version1
7+
* @param {string} version2
8+
* @return {number}
9+
*/
10+
let compareVersion = function(version1, version2) {
11+
let v1array = version1.split('.'),
12+
v2array = version2.split('.'),
13+
exp = new RegExp('^0+$');
14+
15+
while(exp.exec(v1array[v1array.length-1])) v1array.pop();
16+
while(exp.exec(v2array[v2array.length-1])) v2array.pop();
17+
18+
// while(v1array[v1array.length-1].match(exp)) v1array.pop();
19+
// while(v2array[v2array.length-1].match(exp)) v2array.pop();
20+
21+
let v1len = v1array.length,
22+
v2len = v2array.length,
23+
minlen = Math.min(v1len, v2len);
24+
for (let i=0; i<minlen; i++) {
25+
let num1 = Number.parseInt(v1array[i]),
26+
num2 = Number.parseInt(v2array[i]);
27+
28+
if (num1>num2) return 1;
29+
else if (num1<num2) return -1;
30+
}
31+
32+
if (v1len === v2len) {
33+
return 0;
34+
} else {
35+
return v1len>v2len? 1:-1;
36+
}
37+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /