排序陣列是所有元素都按升序排列的陣列。我們給出了一個大小為 N 的陣列和一個包含不同整數的陣列(這意味著每個整數只出現一次)。我們必須檢查陣列是否已排序並以順時針方向旋轉。這裡,如果數組已排序並旋轉,我們必須返回“YES”,否則,我們必須返回“NO”。
注意 - 這裡我們討論的是排序和旋轉意味著至少應該存在一個旋轉。我們不能將排序數組視為排序和旋轉數組。
假設我們已經給了一個大小為 N 的陣列
輸入
N = 5 Array = [5, 1, 2, 3, 4]
輸出
Yes, the given array is sorted and rotated.
說明
陣列是已排序的陣列並旋轉 1 位元
Sorted array = [1, 2, 3, 4, 5] Rotated above sorted array by 1 place, we get = [5, 1, 2, 3, 4]
按 1 個位置排序並旋轉的陣列與輸入數組匹配,因此輸出為「是」。
輸入
N = 6 Array = [6, 5, 1, 2, 3, 4]
輸出
No, the given array is not sorted and rotated array.
說明
給定的陣列是一個未排序且旋轉的陣列
Sorted array = [1, 2, 3, 4, 5, 6] Rotated above sorted array by 1 place, we get = [6, 1, 2, 3, 4, 5] Rotated above sorted array by 2 place, we get = [5, 6, 1, 2, 3, 4] Rotated above sorted array by 3 place, we get = [4, 5, 6, 1, 2, 3] Rotated above sorted array by 4 place, we get = [3, 4, 5, 6, 1, 2] Rotated above sorted array by 5 place, we get = [2, 3, 4, 5, 6, 1]
上面的排序和旋轉數組都不與輸入數組匹配,所以答案是,不。
方法
在這裡我們將討論兩種方法。讓我們在下面的部分中看到它們 -
方法 1:透過尋找樞軸元素(最小數量)來檢查陣列是否已排序和旋轉
這種方法的想法是,我們將找到最小數字,如果我們的陣列已排序並旋轉,則最小數字之前和之後的值應該以排序的方式。
範例
// function to check if the given array is sorted and rotated function check(arr){ var len = arr.length; var mi = Number.MAX_VALUE; // variable to find the smallest number var idx_min = -1; // variable to store the index of smallest number; // traversing over the array to find the minimum element and its index for(var i = 0; i < len; i++){ if (arr[i] < mi){ mi = arr[i]; idx_min = i; } } // traversing over the array to find that all the elements // before the minimum element are sorted for(var i = 1; i < idx_min; i++){ if (arr[i] < arr[i - 1]){ return false; } } // traversing over the array to find that all the elements after the minimum element are sorted for(var i = idx_min + 1; i < len; i++){ if (arr[i] < arr[i - 1]){ return false; } } // checking if the last element of the array is smaller than the first element or not if(arr[len-1] > arr[0]){ return false; } else{ return true; } } // defining the array var arr = [5, 1, 2, 3, 4]; console.log("The given array is: ") console.log(arr); // calling to the function if(check(arr)){ console.log("Yes, the given array is sorted and rotated"); } else{ console.log("No, the given array is not sorted and rotated array") }
時間複雜度 - O(N),其中 N 是陣列的大小。
空間複雜度 - O(1),因為我們沒有使用任何額外的空間。
方法 2:透過計算相鄰反轉來檢查陣列是否已排序和旋轉
這種方法的想法是,我們將遍歷數組並檢查前一個元素是否小於當前元素。對於已排序和旋轉的數組,如果前一個元素大於當前元素,則計數必須為 1,否則,數組不會排序和旋轉。
範例
// function to check if the given array is sorted and rotated function check(arr){ var len = arr.length; var count = 0; // variable to count the adjacent inversions // traversing over the array to find the number of times first element is smaller than second for(var i = 1; i < len; i++){ if (arr[i] < arr[i-1]){ count++; } } // checking if the last element of the array is smaller // than the first element or not and inversion must be equal to 1 if(arr[len-1] > arr[0] || count != 1){ return false; } else{ return true; } } // defining the array var arr = [5, 1, 2, 3, 4]; console.log("The given array is: ") console.log(arr); // calling to the function if(check(arr)){ console.log("Yes, the given array is sorted and rotated"); } else{ console.log("No, the given array is not sorted and rotated array") }
時間複雜度 - O(N),其中 N 是陣列的大小。
空間複雜度 - O(1),因為我們沒有使用任何額外的空間。
結論
在本教程中,我們討論如何檢查陣列是否已排序和旋轉。這裡我們看到了兩種方法,第一種是找到主元(這意味著最小元素),另一種是透過計算相鄰反轉。兩種方法的時間和空間複雜度相同,分別是 O(N) 和 O(1)分別。
以上是JavaScript程式:檢查陣列是否已排序並旋轉的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。

JavaScript起源於1995年,由布蘭登·艾克創造,實現語言為C語言。 1.C語言為JavaScript提供了高性能和系統級編程能力。 2.JavaScript的內存管理和性能優化依賴於C語言。 3.C語言的跨平台特性幫助JavaScript在不同操作系統上高效運行。

JavaScript在瀏覽器和Node.js環境中運行,依賴JavaScript引擎解析和執行代碼。 1)解析階段生成抽象語法樹(AST);2)編譯階段將AST轉換為字節碼或機器碼;3)執行階段執行編譯後的代碼。

Python和JavaScript的未來趨勢包括:1.Python將鞏固在科學計算和AI領域的地位,2.JavaScript將推動Web技術發展,3.跨平台開發將成為熱門,4.性能優化將是重點。兩者都將繼續在各自領域擴展應用場景,並在性能上有更多突破。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器