Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the .
character.
The .
character does not represent a decimal point and is used to separate number sequences.
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.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
这道题目还有0.0.1这种含有好几个点或者是没有.的情况
出处:http://blog.youkuaiyun.com/xudli/article/details/42081113
人家的这个写法真的很巧妙
这个程序的思想是用点号对String进行split,然后把数组对应处转换为数字进行比较,短的那个用0补齐
public class Solution {
public int compareVersion(String version1, String version2) {
if(version1==null || version2==null) return 0;
String[] s1=version1.split("\\."); //注意对于.需要写成\\,而空格不需要
String[] s2=version2.split("\\."); //哪怕是没有.也是可以分割返回的
int l1=s1.length; //数组的长度是s.length
int l2=s2.length;
int i=0;
while(i<l1 || i<l2){ //巧妙之处在于下边两句实现了如果某个数分割后较短话,则用0补齐
int n1=i<l1 ? Integer.parseInt(s1[i]) : 0;
int n2=i<l2 ? Integer.parseInt(s2[i]) : 0;
if(n1<n2) return -1;
else if(n1>n2) return 1;
else
++i;
}
return 0;
}
}
Integer.parseInt()
public static int parseInt(String
s) throws NumberFormatException {
return parseInt(s,10);
}
* @param s a {@code String}
containing the {@code int}
* representation to be parsed
* @return the integer value represented by the argument in decimal.
* @exception NumberFormatException if the string does not contain a
* parsable integer.
i++和++i
总而言之,i++;是一个右值,而 ++i 是一个左值。
加深印象,上述的两条语句可以用下面两个函数表示。
i++ 为
function () {tmp = i;
i = tmp + 1;
return tmp;
}
++i 为
function () {i = i + 1;
return i;
}
i++ 语句需要个临时变量,去存储返回自增前的值。在不影响逻辑的前提下,建议使用 ++i
Java中的split函数的用法——将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
对于分割的字符(串),通常是常见,普通的,没什么问题;但是对某些特殊字符,如果字符(串)正好是正则的一部分,则需要转义才能使用,
这些字符有 | + * ^ $ / | [ ] ( ) - . \等, 因它们是正则表达式中的一部分, 所以如果想用该字符本身, 这些字符需要进行转义才能表示它本身;
例如:
想用 | 竖线去分割某字符,因 | 本身是正则表达式中的一部分,所以需要 \ 去转义,因转义使用 \, 而这个 \ 正好也是正则表达式的字符,所以还得用一个 \ , 所以需要两个 \\
下边这个程序是比较每个以点号分割的区间段里边数字的大小,如果一个比另外一个少点,那么编程思想默认是用0补齐
public class Solution {
public int compareVersion(String version1, String version2) {
if(version1==null || version2==null) return 0;
long l1=version1.length();
long l2=version2.length();
int a=0;
int b=0;
int i=0;
int j=0;
int m=0;
long max=Math.max(l1,l2);
while(m<max){
while(i<l1&&version1.charAt(i)!='.'){
a=a*10+version1.charAt(i)-'0';
++i;
}
++i; //必不可少
while(j<l2&&version2.charAt(j)!='.'){
b=b*10+version2.charAt(j)-'0';
++j;
}
++j; //必不可少
++m;
if(a>b) return 1;
if(a<b) return -1;
a=0;
b=0; //对于a和b要进行清零,必不可少
}
return 0;
}
}
下边这个进行了递归,其中用到了 Integer.valueOf,跟 Integer.parseInt对比学习,还用到了String.substring以及String.contains方法
public
String substring(int beginIndex, int endIndex) {
}
beginIndex
the beginning index, inclusive.
endIndex the ending index, exclusive.
http://www.tuicool.com/articles/3QV7NvV
public class Solution {
public int compareVersion(String version1, String version2) {
if(version1.equals(version2)) return 0;
String sversion1,sversion2; //同一行定义俩变量
int v1,v2;
if(version1.contains(".")){ //String.contains方法
int pos=version1.indexOf("."); //String.indexOf
v1=Integer.valueOf(version1.substring(0,pos)); //substring均是小写
sversion1=version1.substring(pos+1,version1.length());
}
else{
v1=Integer.valueOf(version1);
sversion1="0"; //置为空串
}
if(version2.contains(".")){
int pos=version2.indexOf(".");
v2=Integer.valueOf(version2.substring(0,pos));
sversion2=version2.substring(pos+1,version2.length());
}
else{
v2=Integer.valueOf(version2);
sversion2="0";
}
if(v1>v2) return 1;
if(v1<v2) return -1;
return compareVersion(sversion1,sversion2); //递归
}
}