您好,登錄后才能下訂單哦!
這篇文章給大家介紹C++中怎么實(shí)現(xiàn)一個(gè)LeetCode,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is
[1, 2, 3, 4]
. Therefore its length is 4.
這道題要求求最長(zhǎng)連續(xù)序列,并給定了O(n)復(fù)雜度限制,我們的思路是,使用一個(gè)集合HashSet存入所有的數(shù)字,然后遍歷數(shù)組中的每個(gè)數(shù)字,如果其在集合中存在,那么將其移除,然后分別用兩個(gè)變量pre和next算出其前一個(gè)數(shù)跟后一個(gè)數(shù),然后在集合中循環(huán)查找,如果pre在集合中,那么將pre移除集合,然后pre再自減1,直至pre不在集合之中,對(duì)next采用同樣的方法,那么next-pre-1就是當(dāng)前數(shù)字的最長(zhǎng)連續(xù)序列,更新res即可。這里再說(shuō)下,為啥當(dāng)檢測(cè)某數(shù)字在集合中存在當(dāng)時(shí)候,都要移除數(shù)字。這是為了避免大量的重復(fù)計(jì)算,就拿題目中的例子來(lái)說(shuō)吧,我們?cè)诒闅v到4的時(shí)候,會(huì)向下遍歷3,2,1,如果都不移除數(shù)字的話,遍歷到1的時(shí)候,還會(huì)遍歷2,3,4。同樣,遍歷到3的時(shí)候,向上遍歷4,向下遍歷2,1,等等等。如果數(shù)組中有大量的連續(xù)數(shù)字的話,那么就有大量的重復(fù)計(jì)算,十分的不高效,所以我們要從HashSet中移除數(shù)字,代碼如下:
C++ 解法一:
class Solution { public: int longestConsecutive(vector<int>& nums) { int res = 0; unordered_set<int> s(nums.begin(), nums.end()); for (int val : nums) { if (!s.count(val)) continue; s.erase(val); int pre = val - 1, next = val + 1; while (s.count(pre)) s.erase(pre--); while (s.count(next)) s.erase(next++); res = max(res, next - pre - 1); } return res; } };
Java 解法一:
public class Solution { public int longestConsecutive(int[] nums) { int res = 0; Set<Integer> s = new HashSet<Integer>(); for (int num : nums) s.add(num); for (int num : nums) { if (s.remove(num)) { int pre = num - 1, next = num + 1; while (s.remove(pre)) --pre; while (s.remove(next)) ++next; res = Math.max(res, next - pre - 1); } } return res; } }
我們也可以采用哈希表來(lái)做,剛開(kāi)始HashMap為空,然后遍歷所有數(shù)字,如果該數(shù)字不在HashMap中,那么我們分別看其左右兩個(gè)數(shù)字是否在HashMap中,如果在,則返回其哈希表中映射值,若不在,則返回0,雖然我們直接從HashMap中取不存在的映射值,也能取到0,但是一旦去取了,就會(huì)自動(dòng)生成一個(gè)為0的映射,那么我們這里再for循環(huán)的開(kāi)頭判斷如果存在映射就跳過(guò)的話,就會(huì)出錯(cuò)。然后我們將left+right+1作為當(dāng)前數(shù)字的映射,并更新res結(jié)果,同時(shí)更新num-left和num-right的映射值。
下面來(lái)解釋一下為啥要判斷如何存在映射的時(shí)候要跳過(guò),這是因?yàn)橐坏┠硞€(gè)數(shù)字創(chuàng)建映射了,說(shuō)明該數(shù)字已經(jīng)被處理過(guò)了,那么其周?chē)臄?shù)字很可能也已經(jīng)建立好了映射了,如果再遇到之前處理過(guò)的數(shù)字,再取相鄰數(shù)字的映射值累加的話,會(huì)出錯(cuò)。舉個(gè)例子,比如數(shù)組 [1, 2, 0, 1],當(dāng)0執(zhí)行完以后,HashMap中的映射為 {1->2, 2->3, 0->3},可以看出此時(shí)0和2的映射值都已經(jīng)為3了,那么如果最后一個(gè)1還按照原來(lái)的方法處理,隨后得到結(jié)果就是7,明顯不合題意。還有就是,之前說(shuō)的,為了避免訪問(wèn)不存在的映射值時(shí),自動(dòng)創(chuàng)建映射,我們使用m.count() 先來(lái)檢測(cè)一下,只有存在映射,我們才從中取值,否則就直接賦值為0,參見(jiàn)代碼如下:
C++ 解法二:
class Solution { public: int longestConsecutive(vector<int>& nums) { int res = 0; unordered_map<int, int> m; for (int num : nums) { if (m.count(num)) continue; int left = m.count(num - 1) ? m[num - 1] : 0; int right = m.count(num + 1) ? m[num + 1] : 0; int sum = left + right + 1; m[num] = sum; res = max(res, sum); m[num - left] = sum; m[num + right] = sum; } return res; } };
Java 解法二:
public class Solution { public int longestConsecutive(int[] nums) { int res = 0; Map<Integer, Integer> m = new HashMap<Integer, Integer>(); for (int num : nums) { if (m.containsKey(num)) continue; int left = m.containsKey(num - 1) ? m.get(num - 1) : 0; int right = m.containsKey(num + 1) ? m.get(num + 1) : 0; int sum = left + right + 1; m.put(num, sum); res = Math.max(res, sum); m.put(num - left, sum); m.put(num + right, sum); } return res; } }
關(guān)于C++中怎么實(shí)現(xiàn)一個(gè)LeetCode就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。