【力扣 - 最長連續數組】

題目描述

給定一個未排序的整數數組 nums ,找出數字連續的最長序列(不要求序列元素在原數組中連續)的長度。

請你設計并實現時間復雜度為 O(n) 的算法解決此問題。

示例 1:

輸入:nums = [100,4,200,1,3,2]
輸出:4
解釋:最長數字連續序列是 [1, 2, 3, 4]。它的長度為 4。

示例 2:

輸入:nums = [0,3,7,2,5,8,4,6,0,1]
輸出:9

提示:

0 <= nums.length <= 10^5

-10^9 <= nums[i] <= 10^9

題解

思路

先排序再比較計數;
如果相鄰兩數差一計數,如果相等進入下一層循環判斷;
如果后面數與前一個數既不相等又不比前一個多一,重值計數為1.

代碼

int cmp(const void* a, const void* b)
{// Custom comparison function for qsort to sort integers in ascending orderreturn (long long)*(int*)a - (long long)*(int*)b;
}int longestConsecutive(int* a, int n)
{// Check for edge casesif (a == NULL || n == 0) {return 0;}/* The  qsort  function in C is used to sort an array in ascending order. * It takes the following parameters: * base: Pointer to the array to be sorted. * nmemb: Number of elements in the array. * size: Size in bytes of each element in the array. * compar: Pointer to a comparison function that determines the order of elements. * The comparison function ( compar ) should return an integer less than, * equal to, or greater than zero if the first argument is considered to be * respectively less than, equal to, or greater than the second argument. * void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));*/// Sort the input array 'a' in ascending orderqsort(a, n, sizeof(int), cmp);int t = a[0]; // Initialize 't' with the first element of the sorted arrayint cnt = 1; // Initialize 'cnt' to keep track of the current consecutive sequence lengthint max = 1; // Initialize 'max' to keep track of the maximum consecutive sequence length// Iterate through the sorted array to find the longest consecutive sequencefor (int i = 1; i < n; i++) {if (a[i] == t) {// If the current element is equal to the previous element, skip itcontinue;} else if ((long long)a[i] - t == 1LL) {// If the current element is consecutive to the previous elementt = a[i]; // Update 't' to the current elementcnt++; // Increment the consecutive countif (max < cnt) {max = cnt; // Update 'max' if a longer consecutive sequence is found}} else {// If the current element is not consecutive to the previous elementt = a[i]; // Update 't' to the current elementcnt = 1; // Reset the consecutive count}}return max; // Return the maximum consecutive sequence length
}

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

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

相關文章

Linux命令:uniq命令和wc命令

目錄 1 uniq命令1.1 uniq簡介1.2說明1.3案例1、默認輸出2、輸出重復行3、比較一行中的部分字符4、忽略大小寫5、只顯示唯一的行 2.4 uniq和sort命令配合使用1、文本統計2、統計IP連接數并排序 2 wc命令2.1 wc簡介2.2 說明2.3 案例1、默認輸出2、輸出字節、字符數、單詞數 總結 …

案例介紹:汽車維修系統的信息抽取技術與數據治理應用(開源)

一、引言 在當今汽車產業的快速發展中&#xff0c;軟件已經成為提升車輛性能、安全性和用戶體驗的關鍵因素。從車載操作系統到智能駕駛輔助系統&#xff0c;軟件技術的進步正在重塑我們對汽車的傳統認知。我有幸參與了一個創新項目&#xff0c;該項目專注于開發和集成先進的汽…

關于 svg path 路徑坐標 精度誤差問題

<svg width"2838.739990" height"2482.179932" viewBox"0 0 2838.74 2482.18" fill"none" xmlns"http://www.w3.org/2000/svg" xmlns:xlink"http://www.w3.org/1999/xlink"><path id"矢量 12"…

原理篇-- 定時任務xxl-job-服務端(admin)項目啟動過程--JobRegistryHelper 初始化 (4)

文章目錄 前言一、JobRegistryHelper 作用&#xff1a;二、JobRegistryHelper 源碼介紹&#xff1a;2.1 初始化start() 方法&#xff1a;2.1.1 registryOrRemoveThreadPool 執行器注冊和移除&#xff1a;2.1.2 registryMonitorThread 執行器注冊監控線程&#xff1a; 2.2 toSto…

折線圖實現柱狀陰影背景的demo

這個是一個由官網的基礎折線圖實現的流程&#xff0c;將涉及到的知識點附上個人淺薄的見解&#xff0c;源碼在最后&#xff0c;需要的可自取。 折線圖 成果展示代碼注解參數backgroundColordataZoomlegendtitlexAxisyAxisgridseries 源碼 成果展示 官網的基礎折線圖&#xff…

貓耳語音下載(mediadown)

貓耳語音下載(mediadown) 一、介紹 貓耳語音下載,能夠幫助你下載貓耳音頻節目。如果你是會員,它還能幫你下載會員節目。 二、下載地址 下載:貓耳語音下載(mediadown) 百度網盤下載:貓耳語音下載(mediadown) 三、安裝教程 將下載的文件解壓到D:\xibinhui,D:\Pr…

Unity RectTransform·屏幕坐標轉換

RectTransform轉屏幕坐標 分兩種情況 Canvas渲染模式為Overlay時&#xff0c;使用此方式 public Rect GetScreenCoordinatesOfCorners(RectTransform rt) {var worldCorners new Vector3[4];rt.GetWorldCorners(worldCorners);var result new Rect(worldCorners[0].x,world…

Manomotion 實現AR手勢互動-解決手勢無效的問題

之前就玩過 Manomotion &#xff0c;現在有新需求&#xff0c;重新接入發現不能用了&#xff0c;不管什么辦法&#xff0c;都識別不了手勢&#xff0c;我記得當初是直接調用就可以的。 經過研究發現&#xff0c;新版本SDK改了寫法。下邊就寫一下新版本的調用&#xff0c;并且實…

好書推薦 《Excel函數與公式應用大全for Excel 365 Excel 2021》

一.基本介紹 1.什么是 Excel? Excel 是微軟公司開發的一款電子表格軟件&#xff0c;是 Microsoft Office 套件的一部分。它被廣泛用于數據處理、分析、可視化和管理等方面。Excel 提供了豐富的功能&#xff0c;使用戶能夠創建、編輯、存儲和分享各種類型的數據表格。 2.Exc…

Golang Channel 詳細原理和使用技巧

1.簡介 Channel(一般簡寫為 chan) 管道提供了一種機制:它在兩個并發執行的協程之間進行同步&#xff0c;并通過傳遞與該管道元素類型相符的值來進行通信,它是Golang在語言層面提供的goroutine間的通信方式.通過Channel在不同的 goroutine中交換數據&#xff0c;在goroutine之間…

代碼隨想錄Day66 | 圖的DFS與BFS

代碼隨想錄Day66 | 圖的DFS與BFS DFS797.所有可能的路徑無向圖和有向圖的處理 BFS200.島嶼數量 DFS 文檔講解&#xff1a;代碼隨想錄 視頻講解&#xff1a; 狀態 本質上就是回溯算法。 void dfs(參數) {if (終止條件) {存放結果;return;}for (選擇&#xff1a;本節點所連接的…

『運維備忘錄』之 Shell 內置命令大集合

前言 在 Shell 中&#xff0c;有許多內置命令可用于執行各種任務&#xff0c;包括文件操作、進程管理、環境變量設置等。本文將詳細介紹一些常見的Shell內置命令及其示例用法。 命令描述alias創建命令別名&#xff0c;用于將命令或命令組合關聯到用戶自定義名稱bg將作業放入后…

Qt textBrowser的Html相關

Qt textBrowser的Html相關 Qt textBrowser的Html相關 Qt textBrowser的Html相關 一開始就想要一個簡單的功能&#xff0c;點一下按鈕&#xff0c;添加的文字居中顯示&#xff0c;再點一下按鈕&#xff0c;添加的文字變更顏色居右顯示。 但是&#xff1a; ui->textEdit-&g…

WordPress免費的遠程圖片本地化下載插件nicen-localize-image

nicen-localize-image&#xff08;可在wordpress插件市場搜索下載&#xff09;&#xff0c;是一款用于本地化文章外部圖片的插件&#xff0c;支持如下功能&#xff1a; 文章發布前通過編輯器插件本地化 文章手動發布時自動本地化 文章定時發布時自動本地化 針對已發布的文章…

python類的屬性、方法、靜態方法、靜態方法類內部的調用、直接調用與實例化調用

設計者&#xff1a;ISDF工軟未來 版本&#xff1a;v1.0 日期&#xff1a;2024/3/4 class Restaurant:餐館類def __init__(self,restaurant_name,cuisine_type):#類的屬性self.restaurant_name restaurant_nameself.cuisine_type cuisine_type# self.stregth_level 0def desc…

基于機器學習的密碼強度檢測

項目簡介 利用機器學習對提供的數據集預測用戶輸入的密碼是否為弱密碼。 原始數據集只包含關于弱密碼的信息&#xff0c;并沒有包含強密碼的數據或分類器&#xff0c;這意味著模型無法學習到強密碼的規律!!! 我之所以這樣設計這個示例&#xff0c;其目的是為了向你展示模型的…

python65-Python的循環之for表達式

for表達式用于利用其他區間、元組、列表等可迭代對象創建新的列表。for 表達式的語法格式如下: [表達式 for 循環計數器 in 可迭代對象] 從上面的語法格式可以看出,for表達式與普通for循環的區別 1)在for關鍵字之前定義一個表達式,該表達式通常會包含循環計數器 2)for …

@RestController和@Controller的區別

RestController和Controller是Spring框架中兩個常用的注解&#xff0c;用于標識Controller類 1.Controller&#xff1a;使用Controller注解標記的類是一個典型的MVC控制器&#xff08;controller&#xff09;&#xff0c;用于處理請求并返回視圖。通常在該類的方法上使用Reque…

alzet供應商你值得擁有

在20世紀70年代&#xff0c;ALZE公司研發出來一款巧妙的藥物輸送裝置——Alzet osmotic pump。這款產品如膠囊般精致小巧&#xff0c;它既有膠囊的外表&#xff0c;也具有膠囊的作用。在Alzet osmotic pump中藏有可以裝配藥物溶液的空間。此款膠囊泵如同一個小投遞員&#xff0…

Vue整合three.js 環境從頭搭建 詳細教程

目錄 一、創建Vue項目 二、搭建three.js 三、引入擴展庫OrbitControls 首先我們要有nodejs環境,這里我們用的是17.1.0版本。 一、創建Vue項目 第一步:終端命令創建vue項目 npm create vite@latest