[LeetCode] 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.

Example 1:

Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:

Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:

Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

 

這道題調試了好久,一直不想上網搜別人的解法,因為感覺自己可以做出來,改來改去最后終於通過了,再上網一搜,發現果然和別人的方法不同,小有成就感。我的思路是:由於兩個版本號所含的小數點個數不同,有可能是1和1.1.1比較,還有可能開頭有無效0,比如01和1就是相同版本,還有可能末尾無效0,比如1.0和1也是同一版本。對於沒有小數點的數字,可以默認為最后一位是小數點,而版本號比較的核心思想是相同位置的數字比較,比如題目給的例子,1.2和13.37比較,我們都知道應該顯示1和13比較,13比1大,所以后面的不用再比了,再比如1.1和1.2比較,前面都是1,則比較小數點后面的數字。那么算法就是每次對應取出相同位置的小數點之前所有的字符,把他們轉為數字比較,若不同則可直接得到答案,若相同,再對應往下取。如果一個數字已經沒有小數點了,則默認取出為0,和另一個比較,這樣也解決了末尾無效0的情況。代碼如下:

 

解法一:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int n1 = version1.size(), n2 = version2.size();
        int i = 0, j = 0, d1 = 0, d2 = 0;
        string v1, v2;
        while (i < n1 || j < n2) {
            while (i < n1 && version1[i] != '.') {
                v1.push_back(version1[i++]);
            }
            d1 = atoi(v1.c_str());
            while (j < n2 && version2[j] != '.') {
                v2.push_back(version2[j++]);
            }
            d2 = atoi(v2.c_str());
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            v1.clear(); v2.clear();
            ++i; ++j;
        }
        return 0;
    }
};


當然我們也可以不使用將字符串轉為整型的atoi函數,我們可以一位一位的累加,參加如下代碼:

 

解法二:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int n1 = version1.size(), n2 = version2.size();
        int i = 0, j = 0, d1 = 0, d2 = 0;
        while (i < n1 || j < n2) {
            while (i < n1 && version1[i] != '.') {
                d1 = d1 * 10 + version1[i++] - '0';
            }
            while (j < n2 && version2[j] != '.') {
                d2 = d2 * 10 + version2[j++] - '0';
            }
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            d1 = d2 = 0;
            ++i; ++j;
        }
        return 0;
    }
};

 

由於這道題我們需要將版本號以’.'分開,那么我們可以借用強大的字符串流stringstream的功能來實現分段和轉為整數,使用這種方法寫的代碼很簡潔,如下所示:

 

解法三:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        istringstream v1(version1 + "."), v2(version2 + ".");
        int d1 = 0, d2 = 0;
        char dot = '.';
        while (v1.good() || v2.good()) {
            if (v1.good()) v1 >> d1 >> dot;
            if (v2.good()) v2 >> d2 >> dot;
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            d1 = d2 = 0;
        }
        return 0;
    }
};

 

最后我們來看一種用C語言的字符串指針來實現的方法,這個方法的關鍵是用到將字符串轉為長整型的strtol函數,關於此函數的用法可以參見我的另一篇博客strtol 函數用法。參見代碼如下:

 

解法四:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int res = 0;
        char *v1 = (char*)version1.c_str(), *v2 = (char*)version2.c_str();
        while (res == 0 && (*v1 != '\0' || *v2 != '\0')) {
            long d1 = *v1 == '\0' ? 0 : strtol(v1, &v1, 10);
            long d2 = *v2 == '\0' ? 0 : strtol(v2, &v2, 10);
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            else {
                if (*v1 != '\0') ++v1;
                if (*v2 != '\0') ++v2;
            }
        }
        return res;
    }
};

 

類似題目:

First Bad Version

 

參考資料:

https://leetcode.com/problems/compare-version-numbers/discuss/?orderBy=most_votes

https://leetcode.com/problems/compare-version-numbers/discuss/50774/Accepted-small-Java-solution.

https://leetcode.com/problems/compare-version-numbers/discuss/50788/My-JAVA-solution-without-split

https://leetcode.com/problems/compare-version-numbers/discuss/50804/10-line-concise-solution.-(C%2B%2B)

https://leetcode.com/problems/compare-version-numbers/discuss/50767/My-2ms-easy-solution-with-CC%2B%2B

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM