-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cs
31 lines (30 loc) · 880 Bytes
/
Solution.cs
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
using System;
using System.Linq;
using System.Collections.Generic;
public class Solution {
public int CompareVersion(string version1, string version2) {
var parts1 = version1.Split('.').Select(int.Parse).ToList();
var parts2 = version2.Split('.').Select(int.Parse).ToList();
var minLen = Math.Min(parts1.Count, parts2.Count);
for (var i = 0; i < minLen; ++i)
{
if (parts1[i] < parts2[i])
{
return -1;
}
else if (parts1[i] > parts2[i])
{
return 1;
}
}
if (parts1.Count > parts2.Count && parts1.Skip(minLen).Any(v => v != 0))
{
return 1;
}
if (parts2.Count > parts1.Count && parts2.Skip(minLen).Any(v => v != 0))
{
return -1;
}
return 0;
}
}