标签:leetcode
https://oj.leetcode.com/problems/compare-version-numbers/
http://blog.csdn.net/u012243115/article/details/41969181
public class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null)
return 0; // Invalid input.
// NOTE!!
// Here is the tricky point.
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\.");
for (int i = 0 ; i < v1.length || i < v2.length ; i ++)
{
int value1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
int value2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
int r = Integer.compare(value1, value2);
if (r != 0)
return r;
}
return 0;
}
private int compare(String a, String b)
{
return Integer.compare(Integer.parseInt(a), Integer.parseInt(b));
}
}[LeetCode]165 Compare Version Numbers
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1601307