题解 | #比较版本号#合并升序数组写法
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 比较版本号
* @param version1 string字符串
* @param version2 string字符串
* @return int整型
*/
public int compare (String version1, String version2) {
// write code here
String[] arr1 = version1.split("\\.");
String[] arr2 = version2.split("\\.");
//System.out.println(version1);
int i = 0, j = 0;
while(i < arr1.length && j < arr2.length){
if(Integer.parseInt(arr1[i]) < Integer.parseInt(arr2[j])){
return -1;
}else if(Integer.parseInt(arr1[i]) > Integer.parseInt(arr2[j])){
return 1;
}else{
i++;
j++;
}
}
while(i < arr1.length){
if(Integer.parseInt(arr1[i]) > 0){
return 1;
}else{
i++;
}
}
while(j < arr2.length){
if(Integer.parseInt(arr2[j]) > 0){
return -1;
}
j++;
}
return 0;
}
}
#java#