欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode165.版本比較)

 更新時(shí)間:2021年07月31日 15:39:48   作者:返回主頁(yè)Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode165.版本比較),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

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

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.

這道題調(diào)試了好久,一直不想上網(wǎng)搜別人的解法,因?yàn)楦杏X(jué)自己可以做出來(lái),改來(lái)改去最后終于通過(guò)了,再上網(wǎng)一搜,發(fā)現(xiàn)果然和別人的方法不同,小有成就感。我的思路是:由于兩個(gè)版本號(hào)所含的小數(shù)點(diǎn)個(gè)數(shù)不同,有可能是1和1.1.1比較,還有可能開(kāi)頭有無(wú)效0,比如01和1就是相同版本,還有可能末尾無(wú)效0,比如1.0和1也是同一版本。對(duì)于沒(méi)有小數(shù)點(diǎn)的數(shù)字,可以默認(rèn)為最后一位是小數(shù)點(diǎn),而版本號(hào)比較的核心思想是相同位置的數(shù)字比較,比如題目給的例子,1.2和13.37比較,我們都知道應(yīng)該顯示1和13比較,13比1大,所以后面的不用再比了,再比如1.1和1.2比較,前面都是1,則比較小數(shù)點(diǎn)后面的數(shù)字。那么算法就是每次對(duì)應(yīng)取出相同位置的小數(shù)點(diǎn)之前所有的字符,把他們轉(zhuǎn)為數(shù)字比較,若不同則可直接得到答案,若相同,再對(duì)應(yīng)往下取。如果一個(gè)數(shù)字已經(jīng)沒(méi)有小數(shù)點(diǎn)了,則默認(rèn)取出為0,和另一個(gè)比較,這樣也解決了末尾無(wú)效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;
    }
};

當(dāng)然我們也可以不使用將字符串轉(zhuǎn)為整型的atoi函數(shù),我們可以一位一位的累加,參加如下代碼:

解法二:

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;
    }
};

由于這道題我們需要將版本號(hào)以'.'分開(kāi),那么我們可以借用強(qiáng)大的字符串流stringstream的功能來(lái)實(shí)現(xiàn)分段和轉(zhuǎn)為整數(shù),使用這種方法寫(xiě)的代碼很簡(jiǎn)潔,如下所示:

解法三:

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;
    }
};

最后我們來(lái)看一種用C語(yǔ)言的字符串指針來(lái)實(shí)現(xiàn)的方法,這個(gè)方法的關(guān)鍵是用到將字符串轉(zhuǎn)為長(zhǎng)整型的strtol函數(shù),關(guān)于此函數(shù)的用法可以參見(jiàn)我的另一篇博客strtol 函數(shù)用法。參見(jiàn)代碼如下:

解法四:

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

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode165.版本比較)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)版本比較內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++實(shí)現(xiàn)簡(jiǎn)易UDP網(wǎng)絡(luò)聊天室

    C++實(shí)現(xiàn)簡(jiǎn)易UDP網(wǎng)絡(luò)聊天室

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)易UDP網(wǎng)絡(luò)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Qt編寫(xiě)地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼

    Qt編寫(xiě)地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼

    本文主要介紹了Qt編寫(xiě)地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • C++拷貝構(gòu)造函數(shù)中的陷阱

    C++拷貝構(gòu)造函數(shù)中的陷阱

    這篇文章主要介紹了C++拷貝構(gòu)造函數(shù)中的陷阱,拷貝構(gòu)造函數(shù)大家都比較熟悉,通俗講就是傳入一個(gè)對(duì)象,拷貝一份副本。不過(guò)看似簡(jiǎn)單的東西,實(shí)際不注意的話就會(huì)產(chǎn)生問(wèn)題,下面我們就來(lái)看看C++拷貝構(gòu)造函數(shù)中都有哪些陷阱吧
    2022-01-01
  • C語(yǔ)言編程中統(tǒng)計(jì)輸入的行數(shù)以及單詞個(gè)數(shù)的方法

    C語(yǔ)言編程中統(tǒng)計(jì)輸入的行數(shù)以及單詞個(gè)數(shù)的方法

    這篇文章主要介紹了C語(yǔ)言編程中統(tǒng)計(jì)輸入的行數(shù)以及單詞個(gè)數(shù)的方法,利用最基礎(chǔ)的循環(huán)和判斷語(yǔ)句寫(xiě)成,需要的朋友可以參考下
    2015-11-11
  • C語(yǔ)言中關(guān)于庫(kù)函數(shù) qsort 的模擬實(shí)現(xiàn)過(guò)程

    C語(yǔ)言中關(guān)于庫(kù)函數(shù) qsort 的模擬實(shí)現(xiàn)過(guò)程

    庫(kù)函數(shù)的模擬實(shí)現(xiàn)有利于我們?nèi)ド钊肓私膺@個(gè)函數(shù)內(nèi)部是怎樣實(shí)現(xiàn)的,以及學(xué)習(xí)它的算法,使我們更加了解這個(gè)函數(shù)該怎樣去使用,接下來(lái)我將詳細(xì)的介紹qsort的應(yīng)用及用法,并且用代碼模擬實(shí)現(xiàn)它們的功能
    2021-09-09
  • C語(yǔ)言實(shí)現(xiàn)超市信息管理系統(tǒng)課程設(shè)計(jì)

    C語(yǔ)言實(shí)現(xiàn)超市信息管理系統(tǒng)課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)超市信息管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Qt QFrame的具體使用

    Qt QFrame的具體使用

    本文主要介紹了Qt QFrame的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C語(yǔ)言實(shí)現(xiàn)掃雷附完整代碼

    C語(yǔ)言實(shí)現(xiàn)掃雷附完整代碼

    本文詳細(xì)講解了C語(yǔ)言實(shí)現(xiàn)掃雷并附完整代碼,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • Opencv實(shí)現(xiàn)最小外接矩形和圓

    Opencv實(shí)現(xiàn)最小外接矩形和圓

    這篇文章主要為大家詳細(xì)介紹了Opencv實(shí)現(xiàn)最小外接矩形和圓,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 詳解c++種gmock單元測(cè)試框架

    詳解c++種gmock單元測(cè)試框架

    這篇文章我們給大家分享了關(guān)于c++種gmock單元測(cè)試框架的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2018-08-08

最新評(píng)論