package leetcode; /* Author:Samba Time :2021年9月1日 Data:上午10:01:03 */ //比较版本号 public class t165 { public static void main(String[] args) { Solution165 s = new Solution165(); int result = s.compareVersion("1.0.1", "1"); System.out.println(result); } } class Solution165 { public int compareVersion(String version1, String version2) { String[] version1Str = version1.split("\\."); String[] version2Str = version2.split("\\."); boolean flag = version1Str.length>=version2Str.length; int n = flag?version2Str.length:version1Str.length; //取小的那个 for (int i = 0; i < n; i++) { if(Integer.parseInt(version1Str[i])>Integer.parseInt(version2Str[i])) { return 1; }else if(Integer.parseInt(version1Str[i])<Integer.parseInt(version2Str[i])) { return -1; } } int m = flag?version1Str.length:version2Str.length; for (int i = n; i < m; i++) { if(flag&&Integer.parseInt(version1Str[i])!=0) { return 1; }else if(!flag&&Integer.parseInt(version2Str[i])!=0) { return -1; } } return 0; } }