-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path165. Compare Version Numbers.cpp
35 lines (35 loc) · 1.38 KB
/
165. Compare Version Numbers.cpp
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
class Solution {
public:
int compareVersion(string s1, string s2) {
int n1 = s1.length();
int n2 = s2.length();
int i = 0, j = 0; // variables(pointers) used for moving
int number1 = 0, number2 = 0; // numbers that generated between any two dots of the strings
// start traversing from string 1 and string 2
while(i < n1 || j < n2){
// generating number between dots for string s1
while(i < n1 && s1[i] != '.'){
number1 = number1 * 10 + (s1[i] - '0');
i++;
}
// generating number between dots for string s2
while(j < n2 && s2[j] != '.'){
number2 = number2 * 10 + (s2[j] - '0');
j++;
}
// if number1 is greater than number2, from here return 1
if(number1 > number2) return 1;
// if number1 is less than number2, from here return -1
if(number1 < number2) return -1;
// if equal then we have to proceed further
// again give numbers to zero, as we again generate numbers b/w dots
number1 = 0, number2 = 0;
// move both pointers
i++;
j++;
}
// after traversing whole string, if all the versions are equal, that means
// strings are equal, so return 0
return 0;
}
};