leetcode 128最長連續序列

?

方法一:使用快排:

//排序法,時間O(nlogn),使用STL,只是驗證一下思想,非正解;
class Solution {
public:int longestConsecutive(vector<int>& nums) {sort(nums.begin(),nums.end());int res=0;for(int i=0;i<nums.size();i++){int step=0,len=1;while(i+step!=nums.size()-1&&nums[i+step+1]-nums[i+step]<=1){if(nums[i+step]+1==nums[i+step+1]) len++;step++;}res=max(res,len);i+=step;}return res;}
};

?方法二:使用并查集如題所說達到O(n)

?

方法三:使用哈希表O(n)

//哈希表結合染色,建立一個哈希表,然后遍歷之后計數每個元素周圍所有相鄰元素并染色,記錄個數;O(n)復雜度
class Solution {
public:int longestConsecutive(vector<int>& nums) {int len=nums.size();if(len<=1) return len;unordered_map<int,int> m;int res=0;for(int n:nums)m[n]=1;for(int n:nums){int i=n,j=n;int cnt=1;if(m[n]==0)continue;elsem[n]=0;while(m[i+1]==1){i++;m[i]=0;}while(m[j-1]==1){j--;m[j]=0;}cnt=i-j+1;res=cnt>res?cnt:res;}return res;}
};

別人家的哈希表:

/****
通過哈希表記錄邊界信息neither i+1 nor i-1 has been seen: m[i]=1;both i+1 and i-1 have been seen: extend m[i+m[i+1]] and m[i-m[i-1]] to each other;only i+1 has been seen: extend m[i+m[i+1]] and m[i] to each other;only i-1 has been seen: extend m[i-m[i-1]] and m[i] to each other.*****/
class Solution {
public:int longestConsecutive(vector<int>& nums) {int len=nums.size();if(len<=1) return len;unordered_map<int,int> m;int res=0;for(int i:nums){if(m[i]) continue;//后面表達式為將m[i]和這一段連續序列的邊界全部賦值為他的長度//邊界只可能為m[i-m[i-1]]到m[i+m[i+1]],m[i]到m[i+m[i+1]],m[i-m[i-1]]到m[i]這幾種情況,因此更新三者的值為新連續序列的長度即可//又因為沒有的元素哈希值為0,所以m[i]左右元素的m[i-1]+m[i+1]+1為新序列的長度;res=max(res,m[i]=m[i+m[i+1]]=m[i-m[i-1]]=m[i-1]+m[i+1]+1);}return res;}
};

?別人家的使用hashset 和 hashtable:

hashset:

/****
通過哈希set將nums轉化為哈希set,然后對哈希set進行遍歷,尋找連續片段的左邊界,然后num+1進行遍歷。
可以證明每個元素將被訪問2遍,for中一遍,while一遍,所以time O(n),space O(n);
由于int會產生越界,可以使用long,也可以進行邊界檢測,INT_MAX break;*****/
class Solution {
public:int longestConsecutive(vector<int>& nums) {int res=0;unordered_set<int> h(nums.begin(),nums.end());for(int num:nums){int l=0;if(!h.count(num-1)){while(h.count(num)!=0){++l;if(num==INT_MAX) break;++num;}res=res>l?res:l;}}return res;}
};

hashtable:

/****
通過哈希tablesolution 1: hashtable (key,len)
case1: no neighboors
h[num]=1;
case2: one neighboor
l=h[num-1] or r=h[num+1]
h[num]=h[num-1]=l+1 or h[num]=h[num+1]=r+1
case3: two neighboors
l=h[num-1]
r=h[num+1]
h[num]=h[num-1]=h[num+1]=l+r+1*****/
class Solution {
public:int longestConsecutive(vector<int>& nums) {int res=0;unordered_map<int,int> h;for(int num:nums){if(h[num]!=0) continue;int l=h[num-1];int r=h[num+1];int t=l+r+1;h[num]=h[num+r]=h[num-l]=t;res=res>t?res:t;}return res;}
};

?哈希set

/****
通過哈希set將nums轉化為哈希set,然后對哈希set進行遍歷,尋找連續片段的左邊界,然后num+1進行遍歷。
可以證明每個元素將被訪問2遍,for中一遍,while一遍,所以time O(n),space O(n);
由于int會產生越界,可以使用long,也可以進行邊界檢測,INT_MAX break;*****/
class Solution {
public:int longestConsecutive(vector<int>& nums) {int res=0;unordered_set<int> h(nums.begin(),nums.end());for(int num: nums){int l=0;if(h.count(num-1)==0){while(h.count(num)>0){l++;if(num==INT_MAX) break;num++;}}res=res>l?res:l;}return res;}
};

?

有道詞典
solution 1: has ...
詳細X
解決方案1:哈希表(關鍵,len) case1:沒有neighboorsh (num) = 1,例2:一個neighboorl = h [num-1]或r = h (num + 1) h (num) = h [num-1] = l + 1或h (num) = h (num + 1) = r + 1 case3:兩個neighboorsl = h [num-1] r = h (num + 1) h (num) = h [num-1] = h (num + 1) = l + r + 1         * * * * * /類解決方案{公眾:int longestConsecutive(向量< int > & num) {int res = 0; unordered_map < int, int > h;為(int num: num){如果(h (num) ! = 0)繼續;int l = h [num-1]; int r = h (num + 1); int t = l + r + 1; h (num) = h (num + r) = h [num-l] = t; res = res > t ? res: t;}返回res;}};

轉載于:https://www.cnblogs.com/joelwang/p/10876881.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279517.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279517.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279517.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

8月19學習練習[兩三個TableView并排顯示]

要求&#xff1a;在一個view中顯示兩個tableView&#xff0c;要求左右顯示的內容以及行數不一樣&#xff0c;且左邊每行顯示兩張圖片&#xff08;分別3個一輪回&#xff0c;2個一輪回&#xff09;并且顯示中國的城市名&#xff0c;右邊顯示水果名。點擊時分別顯示城市名或水果名…

word多級列表創建目錄_如何在Microsoft Word中創建和使用多級列表

word多級列表創建目錄Microsoft Word lets you easily create and format multilevel lists in your documents. You can choose from a variety of formatting options, including bulleted, numbered, or alphabetized lists. Let’s take a look. Microsoft Word使您可以輕松…

如何將多個Android Wear手表與單個手機配對

When it comes to “regular” wristwatches, a lot of people have different watches for different activities. It makes sense—a sporty watch for the gym, a nicer watch for the office, and a casual watch for everything else. If you want to live this life with…

Android系統的智能指針(輕量級指針、強指針和弱指針)的實現原理分析(3)...

提供引用計數器的類RefBase我們就暫時介紹到這里&#xff0c;后面我們再結合智能指針類一起分析&#xff0c;現在先來看看強指針類和弱指針類的定義。強指針類的定義我們在前面介紹輕量級指針的時候已經見到了&#xff0c;就是sp類了&#xff0c;這里就不再把它的代碼列出來了。…

ref:下一個項目為什么要用 SLF4J

ref:http://blog.mayongfa.cn/267.html 阿里巴巴 Java 開發手冊 前幾天阿里巴巴在云棲社區首次公開阿里官方Java代碼規范標準&#xff0c;就是一個PDF手冊&#xff0c;有命名規范&#xff0c;讓你知道自己原來取的每一個類名、變量名都是爛名字&#xff0c;真替你家未來孩子擔心…

洛谷P5055 【模板】可持久化文藝平衡樹(FHQ Treap)

題面 傳送門 題解 日常敲板子.jpg //minamoto #include<bits/stdc.h> #define R register #define inline __inline__ __attribute__((always_inline)) #define fp(i,a,b) for(R int i(a),I(b)1;i<I;i) #define fd(i,a,b) for(R int i(a),I(b)-1;i>I;--i) #define …

計算機突然藍屏無法啟動_為什么計算機無法立即啟動?

計算機突然藍屏無法啟動With the newer, more powerful hardware and improved operating systems that we have available to use these days, why does it still take as long as it does to fully boot a computer up each time? 借助我們如今可以使用的更新&#xff0c;更…

CCNA課堂練習:OSPF的介紹及配置

CCNA淺談OSPF的配置 今天我們來談談路由器OSPF的配置&#xff0c;那我先來介紹一下OSPF的特點&#xff1a;1、對網絡發生的變化能夠快速響應2、當網絡發生變化的時候發送觸發式更新?3、支持VLAN 4、管理方便ospf引用了區域的概念&#xff0c;區域分兩種&#xff1a;1、骨干區域…

vcenter 6.7 (vcsa)部署指南

閑言少敘&#xff0c;直達心靈。 一、部署提要1.1 vCenter Server Appliance(VCSA )6.7下載地址https://pan.baidu.com/s/1WUShsC23E2qIIBg7MPR87w 6lzb 二、安裝部署VCSA分為兩個階段安裝&#xff0c;下面我們開始第一階段2.1 打開之后&#xff0c;直接點擊安裝按鈕2.2部署設備…

如何停止Internet Explorer 11的建議站點?

Internet Explorer automatically suggests addresses and search results based on the partial address you’re typing out. If this feature irritates you, read on as we learn how to turn it off. Internet Explorer會根據您鍵入的部分地址自動建議地址和搜索結果。 如…

什么是SG?+SG模板

先&#xff0c;定義一下 狀態Position P 先手必敗 N x先手必勝 操作方法&#xff1a; 反向轉移 相同狀態 不同位置 的一對 相當于無 對于ICG游戲&#xff0c;我們可以將游戲中每一個可能發生的局面表示為一個點。并且若存在局面i和局面j&#xff0c;且j是i的后繼局面(即局面i可…

【桌面虛擬化】之三 Persistent vs NonP

作者&#xff1a;范軍 &#xff08;Frank Fan&#xff09; 新浪微博&#xff1a;frankfan7 在【桌面虛擬化】之二類型及案例中我們探討了桌面虛擬化的兩種架構&#xff0c;HostedVirtual Desktop (VDI) 和 Published Desktop/App. 本文深入分析其中VDI的兩種桌面類型&#xff0…

H5 video 開發問題及相關知識點

相關鏈接&#xff1a;H5 video 的使用H5 video 全屏播放? video點播與直播H5 video目前所有瀏覽器都支持的視頻格式是MP4格式&#xff0c;所以mp4應當是點播web視頻的首選格式。而在直播視頻上&#xff0c;H5 video只在移動端原生支持HLS流的直播視頻(Mac safari video標簽也支…

Mybatis-Generator自動生成XML文件以及接口和實體類

整合了MySQL和Oracle配置文件生成方法 這個是整個文件夾的下載地址&#xff1a;http://www.codepeople.cn/download 主要給大家介紹一下generatorConfig.xml文件的配置&#xff0c;以及生成后的文件。 generatorConfig.xml <?xml version"1.0" encoding"UTF…

如何在Windows 10上設置默認Linux發行版

Windows 10 now allows you to install multiple Linux environments, starting with the Fall Creators Update. If you have multiple Linux environments, you can set your default and switch between them. Windows 10現在允許您從Fall Creators Update開始安裝多個Linux…

mysql全備份+增量備份筆記總結

備份基礎知識 冷備&#xff08;cold backup&#xff09;&#xff1a;需要關mysql服務&#xff0c;讀寫請求均不允許狀態下進行&#xff1b; 溫備&#xff08;warm backup&#xff09;&#xff1a; 服務在線&#xff0c;但僅支持讀請求&#xff0c;不允許寫請求&#xff1b; 熱備…

pjax學習

PJAX 介紹 紅薯 發布于 2012/04/11 22:06閱讀 61K收藏 116評論 11jQuery.Pjax kissy開發四年只會寫業務代碼&#xff0c;分布式高并發都不會還做程序員&#xff1f;->>> 介紹 pushState是一個可以操作history的api&#xff0c;該api的介紹和使用請見這里&#xff1a…

SQL Server 2000詳細安裝過程及配置

說明&#xff1a;這篇文章是幾年前我發布在網易博客當中的原創文章&#xff0c;但由于網易博客現在要停止運營了&#xff0c;所以我就把這篇文章搬了過來&#xff0c;雖然現如今SQL Server 2000軟件早已經過時了&#xff0c;但仍然有一部分人在使用它&#xff0c;尤其是某些高校…

移動應用ios和網頁應用_如何在iOS上一次移動多個應用

移動應用ios和網頁應用Apple doesn’t really believe in detailed instruction manuals, so some handy tricks slip through the cracks. One such trick we’ve recently discovered is that you can move multiple app icons at once on iOS. Here’s how. Apple并不真正相…

如何將內核靜態庫編譯連接到驅動程序中去【轉】

轉自&#xff1a;http://blog.csdn.net/ganjianfeng2003/article/details/8089551 如何將內核靜態庫編譯連接到驅動程序中去 2010-12-07 08:27 331人閱讀 評論(1) 收藏 舉報 http://blog.chinaunix.net/u2/61663/showart_2404744.html 剛上郵箱的時候發現一位網友向我詢問這個問…