File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments