Compare numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return-1, otherwise ret Urn 0. You May Assume that the version strings is non-empty and contain only digits and The . character. The character does not represent a decimal point and was used to separate number sequences. for Instance, 2.5 is not " Both and a half "or" half-to-version three ", it is the fifth Second-level revision of the second First-level revision.< /span> Here are an example of version numbers ordering: 0.1 < 1.1 < 1.2 < 13.37
Credits: Special thanks to @ts for adding this problem and creating all test cases. 165
Problems 1. Whether the string contains a. character 2. The string contains multiple. Characters 3. The string suffix that contains the. Character is 0, for example, the comparison between 1.0.0 and 1
Java strings when processing. characters, be sure to add "\ \", "\ \".
public class Solution { public int Compareversion (string version1, String version2) { String w1[]=version1.split ("\ \"); String w2[]=version2.split ("\ \"); int l1=w1.length; int l2=w2.length; int l=l1; if (L2<L1) L=L2; int i=0; for (i=0;i<l;i++) { int A=integer.parseint (w1[i]); int B=integer.parseint (w2[i]); if (a<b) return-1; if (a>b) return 1; } if (L1<L2) { for (i=l;i<l2;i++) if (Integer.parseint (W2[i]) >0) return-1; return 0; } else if (L1>L2) { for (i=l;i<l1;i++) if (Integer.parseint (W1[i]) >0) return 1; return 0; } Else return 0;
} }
|