File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int conv(string s) //convert a string to number
4+ {
5+ int ans=0;
6+ for(int i=0;i<s.size();i++)
7+ {
8+ int num = s[i]-'0';
9+ ans*=10;
10+ ans+=num;
11+ }
12+ return ans;
13+ }
14+ vector<int> solve(string s)
15+ {
16+ s+="."; //add dot at end since we are getting revision by finding str between 2 dots
17+ int n = s.size();
18+ vector<int> ans;
19+ string str = "";
20+
21+
22+ for(int i=0;i<n;i++){
23+ if(s[i]=='.') //once we get a dot
24+ {
25+ int num = conv(str); //convert str to num
26+ ans.push_back(num);
27+ str=""; //reset str
28+ }
29+ else{
30+ str+=s[i];
31+ }
32+ }
33+
34+ return ans;
35+
36+ }
37+ int compareVersion(string version1, string version2) {
38+
39+ vector<int> v1 = solve(version1);
40+ vector<int> v2 = solve(version2);
41+
42+ int n = v1.size();
43+ int m = v2.size();
44+
45+ if(n<m){
46+ for(int i=1;i<=(m-n);i++) v1.push_back(0); //if v1 is smaller than v2, add 0 to v1 till same size
47+ }else{
48+ for(int i=1;i<=(n-m);i++) v2.push_back(0); //if v2 is smaller than v1, add 0s to v2 till same size
49+ }
50+ int sz = v1.size();
51+ for(int i=0;i<sz;i++)
52+ {
53+ if(v1[i]>v2[i]) return 1;
54+ else if(v1[i]<v2[i]) return -1;
55+ }
56+
57+ return 0;
58+
59+ }
60+ };
You can’t perform that action at this time.
0 commit comments