首页 > 代码库 > 165. Compare Version Numbers

165. Compare Version Numbers

题目:

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

链接: http://leetcode.com/problems/compare-version-numbers/

6/16/2017

3ms, 21%

注意的问题

1. 第6,7行,string.split()对于以dot分隔的话,需要转义成\\.

2. Integer.parseInt(), Integer.toString()分别用于string->int, int->string

3. 前一部分每一块需要比较empty string

4. 如果长度不等的话,不但需要比较长度,还要比较多余长度里面是不是都是0,如果是的话2个version还是一样的。

 1 public class Solution {
 2     public int compareVersion(String version1, String version2) {
 3         if (version1 == null || version2 == null) {
 4             return 0;
 5         }
 6         String[] v1 = version1.split("\\.");
 7         String[] v2 = version2.split("\\.");
 8         
 9         for (int i = 0, j = 0; i < v1.length && j < v2.length; i++, j++) {
10             if (v1[i].equals("") && v2[i].equals("")) {
11                 continue;
12             }
13             if (v1[i].equals("") || Integer.parseInt(v1[i]) < Integer.parseInt(v2[i])) {
14                 return -1;
15             } else if (v2[i].equals("") || Integer.parseInt(v2[i]) < Integer.parseInt(v1[i])) {
16                 return 1;
17             }
18         }
19         if (v1.length > v2.length) {
20             for (int i = v2.length; i < v1.length; i++) {
21                 if (Integer.parseInt(v1[i]) != 0) {
22                     return 1;
23                 }
24             }
25             return 0;
26         } else if (v1.length < v2.length) {
27             for (int i = v1.length; i < v2.length; i++) {
28                 if (Integer.parseInt(v2[i]) != 0) {
29                     return -1;
30                 }
31             }
32             return 0;            
33         }
34         return 0;
35     }
36 }

别人的答案,2个版本都比我的好,一个是将empty string变为0,一个直接用数字来计算。

http://www.cnblogs.com/yrbbest/p/4491633.html

其他答案

https://discuss.leetcode.com/topic/6238/accepted-small-java-solution

 1 public int compareVersion(String version1, String version2) {
 2     String[] levels1 = version1.split("\\.");
 3     String[] levels2 = version2.split("\\.");
 4     
 5     int length = Math.max(levels1.length, levels2.length);
 6     for (int i=0; i<length; i++) {
 7         Integer v1 = i < levels1.length ? Integer.parseInt(levels1[i]) : 0;
 8         Integer v2 = i < levels2.length ? Integer.parseInt(levels2[i]) : 0;
 9         int compare = v1.compareTo(v2);
10         if (compare != 0) {
11             return compare;
12         }
13     }
14     
15     return 0;
16 }

https://discuss.leetcode.com/topic/11410/my-2ms-easy-solution-with-c-c

 1 int compareVersion(string version1, string version2) {
 2     int i = 0; 
 3     int j = 0;
 4     int n1 = version1.size(); 
 5     int n2 = version2.size();
 6     
 7     int num1 = 0;
 8     int num2 = 0;
 9     while(i<n1 || j<n2)
10     {
11         while(i<n1 && version1[i]!=‘.‘){
12             num1 = num1*10+(version1[i]-‘0‘);
13             i++;
14         }
15         
16         while(j<n2 && version2[j]!=‘.‘){
17             num2 = num2*10+(version2[j]-‘0‘);;
18             j++;
19         }
20         
21         if(num1>num2) return 1;
22         else if(num1 < num2) return -1;
23         
24         num1 = 0;
25         num2 = 0;
26         i++;
27         j++;
28     }
29     
30     return 0;
31 }

更多讨论

https://discuss.leetcode.com/category/173/compare-version-numbers

165. Compare Version Numbers