8000 Compare Version Numbers · leetcoders/LeetCode@de27c6a · GitHub
[go: up one dir, main page]

Skip to content

Commit de27c6a

Browse files
author
JINGUIWANG
committed
Compare Version Numbers
Change-Id: I9c03f888e46db11757f4c8e8f14b29dbd9505f09
1 parent 7722437 commit de27c6a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

CompareVersionNumbers.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 15, 2014
4+
Problem: Compare Version Numbers
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/compare-version-numbers/
7+
Notes:
8+
Compare two version numbers version1 and version1.
9+
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
10+
11+
You may assume that the version strings are non-empty and contain only digits and the . character.
12+
The . character does not represent a decimal point and is used to separate number sequences.
13+
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
14+
15+
Here is an example of version numbers ordering:
16+
17+
0.1 < 1.1 < 1.2 < 13.37
18+
19+
Solution: ...
20+
*/
21+
class Solution {
22+
public:
23+
int compareVersion(string version1, string version2) {
24+
int v1len = version1.size(), v2len = version2.size();
25+
for (int i = 0, j = 0; (i < v1len || j < v2len); ) {
26+
long long a = 0, b =0;
27+
while (i < v1len && version1[i] != '.') {
28+
a = a * 10 + version1[i++] - '0';
29+
}
30+
++i;
31+
while (j < v2len && version2[j] != '.') {
32+
b = b * 10 + version2[j++] - '0';
33+
}
34+
++j;
35+
if (a > b) return 1;
36+
if (a < b) return -1;
37+
}
38+
return 0;
39+
}
40+
};

0 commit comments

Comments
 (0)
0