首页 > 代码库 > LeetCode - Compare Version Numbers

LeetCode - Compare Version Numbers

Compare Version Numbers

2015.1.23 15:27

Compare two version numbers version1 and version1.
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

Solution:

  Seems this kind of questions is closest to real problems you have to deal with: trivial, dull and sometimes complicated. But don‘t take it that way, otherwise you would be overconfident and soon enough shocked by how bad you handled it.

  "0" would be the first edge case, "0.0" or "0.0.0" may be illegal, for it should be equal to "0", resulting in ambiguity.

  Here‘re the few occasions I thought about:

    0 < 1

    0 < 0.2

    1.2 < 3.1

    1.234 < 1.235

    1.1 < 1.1.3

    0.2.0? Illegal! That trailing zero shouldn‘t be there.

  Split the version numbers, and do what you have to do then.

Accepted code:

  1 // 1CE, 3WA, 1AC, so boring.  2 class Solution {  3 public:  4     int compareVersion(string version1, string version2) {  5         splitNumber(version1, s1);  6         splitNumber(version2, s2);  7         normalize(s1);  8         normalize(s2);  9          10         int n1, n2, nm; 11          12         n1 = (int)s1.size(); 13         n2 = (int)s2.size(); 14         nm = n1 < n2 ? n1 : n2; 15          16         int i; 17         int res; 18         for (i = 0; i < nm; ++i) { 19             res = compare(s1[i], s2[i]); 20             if (res != 0) { 21                 return res; 22             } 23         } 24         if (n1 > n2) { 25             return 1; 26         } else if (n1 < n2) { 27             return -1; 28         } else { 29             return 0; 30         } 31     } 32 private: 33     vector<string> s1, s2; 34      35     void splitNumber(const string &s, vector<string> &ss) { 36         int len = (int)s.length(); 37         int i, j, k; 38          39         ss.clear(); 40          41         ss.push_back(string()); 42         i = 0; 43         j = 0; 44         k = 0; 45         while (i < len) { 46             if (s[i] == .) { 47                 ss.push_back(string()); 48                 ++k; 49                 j = 0; 50             } else { 51                 ss[k].push_back(s[i]); 52                 ++j; 53             } 54             ++i; 55         } 56     } 57      58     int compare(const string &n1, const string &n2) { 59         if (n1.length() > n2.length()) { 60             return 1; 61         } else if (n1.length() < n2.length()) { 62             return -1; 63         } 64          65         int i; 66         int len = (int)n1.length(); 67          68         for (i = 0; i < len; ++i) { 69             if (n1[i] > n2[i]) { 70                 return 1; 71             } else if (n1[i] < n2[i]) { 72                 return -1; 73             } 74         } 75          76         return 0; 77     } 78      79     string trimZero(const string &s) { 80         int i, len; 81         string s1; 82          83         len = (int)s.length(); 84         i = 0; 85         while (i < len - 1 && s[i] == 0) { 86             ++i; 87         } 88         while (i < len) { 89             s1.push_back(s[i]); 90             ++i; 91         } 92          93         return s1; 94     } 95      96     void normalize(vector<string> &ss) { 97         int i; 98         int n = (int)ss.size(); 99         100         for (i = 0; i < n; ++i) {101             ss[i] = trimZero(ss[i]);102         }103         while (ss.size() > 1u && ss[ss.size() - 1] == "0") {104             ss.pop_back();105         }106     }107 };

 

LeetCode - Compare Version Numbers