C++實(shí)現(xiàn)LeetCode165.版本比較)
[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; } };
類似題目:
參考資料:
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
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode165.版本比較)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)版本比較內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(172.求階乘末尾零的個(gè)數(shù))
- C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))
- C++實(shí)現(xiàn)LeetCode(169.求大多數(shù))
- C++實(shí)現(xiàn)LeetCode(171.求Excel表列序號(hào))
- C++實(shí)現(xiàn)LeetCode(168.求Excel表列名稱)
- C++實(shí)現(xiàn)LeetCode(167.兩數(shù)之和之二 - 輸入數(shù)組有序)
- C++實(shí)現(xiàn)LeetCode(166.分?jǐn)?shù)轉(zhuǎn)循環(huán)小數(shù))
- C++實(shí)現(xiàn)LeetCode(173.二叉搜索樹(shù)迭代器)
相關(guān)文章
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-07Qt編寫(xiě)地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼
本文主要介紹了Qt編寫(xiě)地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12C語(yǔ)言編程中統(tǒng)計(jì)輸入的行數(shù)以及單詞個(gè)數(shù)的方法
這篇文章主要介紹了C語(yǔ)言編程中統(tǒng)計(jì)輸入的行數(shù)以及單詞個(gè)數(shù)的方法,利用最基礎(chǔ)的循環(huán)和判斷語(yǔ)句寫(xiě)成,需要的朋友可以參考下2015-11-11C語(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-09C語(yǔ)言實(shí)現(xiàn)超市信息管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)超市信息管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03