leetcode 153. 尋找旋轉排序數組中的最小值(二分查找)

已知一個長度為 n 的數組,預先按照升序排列,經由 1 到 n 次 旋轉 后,得到輸入數組。例如,原數組 nums = [0,1,2,4,5,6,7] 在變化后可能得到:
若旋轉 4 次,則可以得到 [4,5,6,7,0,1,2]
若旋轉 4 次,則可以得到 [0,1,2,4,5,6,7]
注意,數組 [a[0], a[1], a[2], …, a[n-1]] 旋轉一次 的結果為數組 [a[n-1], a[0], a[1], a[2], …, a[n-2]] 。

給你一個元素值 互不相同 的數組 nums ,它原來是一個升序排列的數組,并按上述情形進行了多次旋轉。請你找出并返回數組中的 最小元素 。

示例 1:

輸入:nums = [3,4,5,1,2]
輸出:1
解釋:原數組為 [1,2,3,4,5] ,旋轉 3 次得到輸入數組。

解題思路

分成3鐘情況討論
7 8 9 1 2 3 4 5 6
nums[l]<nums[mid]
情況一:nums[l]=1 nums[mid]=3 nums[r]=5 在左邊區間招找
情況二:nums[l]=7 nums[mid]=9 nums[r]=2 在右邊區間找

7 8 9 1 2 3 4 5 6
nums[l]=7 nums[mid]=3
nums[l]>nums[mid] 所以可以確定最小值只會在左邊區間產生

2 3 4 5 6 1
nums[l]=nums[mid]=6 nums[r]=1
nums[l]==nums[mid] 結果只會在nums[l]和nums[r]中產生,選出最小值即可

代碼

class Solution {public int findMin(int[] nums) {int l=0,r=nums.length-1;while (l<=r){int mid=(r-l)/2+l;if(nums[l]==nums[mid]) {return Math.min(nums[r],nums[l]);}else if(nums[l]<nums[mid]){if(nums[r]<nums[l])l=mid;else  r=mid;}else {r=mid;}}return nums[r];}
}

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

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

相關文章

test1

test1 轉載于:https://www.cnblogs.com/Forever77/p/11434403.html

打印風車旋轉效果

1 while True: 2 for i in["/","-","\\","|"]: 3 print "%s\r" %i, 轉載于:https://www.cnblogs.com/feifei-cyj/p/7469333.html

深度學習數據自動編碼器_如何學習數據科學編碼

深度學習數據自動編碼器意見 (Opinion) When I first wanted to learn programming, I coded along to a 4 hour long YouTube tutorial.剛開始學習編程時&#xff0c;我編寫了長達4個小時的YouTube教程。 “Great,” I thought after finishing the course. “I know how to …

Angular 5.0 學習2:Angular 5.0 開發環境的搭建和新建第一個ng5項目

1.安裝Node.js 在開始工作之前&#xff0c;我們必須設置好開發環境。如果你的機器上還沒有Node.js和npm&#xff0c;請先安裝它們。去Node.js的官網&#xff0c;https://nodejs.org/en/&#xff0c;點擊下載按鈕&#xff0c;下載最新版本&#xff0c;直接下一步下一步安裝即可&…

leetcode 154. 尋找旋轉排序數組中的最小值 II(二分查找)

已知一個長度為 n 的數組&#xff0c;預先按照升序排列&#xff0c;經由 1 到 n 次 旋轉 后&#xff0c;得到輸入數組。例如&#xff0c;原數組 nums [0,1,4,4,5,6,7] 在變化后可能得到&#xff1a; 若旋轉 4 次&#xff0c;則可以得到 [4,5,6,7,0,1,4] 若旋轉 7 次&#xff0…

robot:根據條件主動判定用例失敗或者通過

場景&#xff1a; 當用例中的斷言部分需要滿足特定條件時才會執行&#xff0c;如果不滿足條件時&#xff0c;可以主動判定該用例為passed狀態&#xff0c;忽略下面的斷言語句。 如上圖場景&#xff0c;當每月1號時&#xff0c;表中才會生成上月數據&#xff0c;生成后數據不會再…

golang go語言_在7小時內學習快速簡單的Go編程語言(Golang)

golang go語言The Go programming language (also called Golang) was developed by Google to improve programming productivity. It has seen explosive growth in usage in recent years. In this free course from Micheal Van Sickle, you will learn how to use Go step…

使用MUI框架,模擬手機端的下拉刷新,上拉加載操作。

套用mui官方文檔的一句話&#xff1a;“開發者只需關心業務邏輯&#xff0c;實現加載更多數據即可”。真的是不錯的框架。 想更多的了解這個框架&#xff1a;http://dev.dcloud.net.cn/mui/ 那么如何實現下拉刷新&#xff0c;上拉加載的功能呢&#xff1f; 首先需要一個容器&am…

圖深度學習-第1部分

有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as the videos. Of cou…

Git上傳項目到github

2019獨角獸企業重金招聘Python工程師標準>>> Git入門 個人理解git就是一個上傳工具&#xff0c;同時兼具和svn一樣的版本控制功能&#xff08;此解釋純屬本人個人觀點&#xff09; Github是什么 github就是一個分布式版本管理系統&#xff08;反正我就是這么認為的…

ionic4 打包ios_學習Ionic 4并開始創建iOS / Android應用

ionic4 打包iosLearn how to use Ionic 4 in this full course for beginners from Awais Mirza. Ionic Framework is the free, open source mobile UI toolkit for developing high-quality cross-platform apps for native iOS, Android, and the web—all from a single Ja…

robot:當用例失敗時執行關鍵字(發送短信)

使用場景&#xff1a; 當用例失敗時需要通知對應人員&#xff0c;則需要在Teardown中&#xff0c;使用關鍵字Run Keyword If Test Failed Send Message關鍵字為自定義關鍵字&#xff0c;${content}為短信內容&#xff0c;${msg_receiver}為短信接收者列表。 當然執行成功時需要…

leetcode 263. 丑數

給你一個整數 n &#xff0c;請你判斷 n 是否為 丑數 。如果是&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 丑數 就是只包含質因數 2、3 和/或 5 的正整數。 示例 1&#xff1a; 輸入&#xff1a;n 6 輸出&#xff1a;true 解釋&#xff1a;6 2 3 …

NTP同步

RedHat Linux NTP實施步驟1、 查看本系統與NTP服務器的時間偏差 ntpdate -d 192.168.142.114 [rootzabbix-proxy ~]# ntpdate -d 192.168.142.114 24 Aug 17:26:45 ntpdate[3355]: ntpdate 4.2.6p51.2349-o Fri Apr 13 12:52:28 UTC 2018 (1) Looking for host 192.168.142.…

項目經濟規模的估算方法_估算英國退歐的經濟影響

項目經濟規模的估算方法On June 23 2016, the United Kingdom narrowly voted in a country-wide referendum to leave the European Union (EU). Economists at the time warned of economic losses; the Bank of England produced estimates that that GDP could be as much …

Oracle宣布新的Java Champions

\看新聞很累&#xff1f;看技術新聞更累&#xff1f;試試下載InfoQ手機客戶端&#xff0c;每天上下班路上聽新聞&#xff0c;有趣還有料&#xff01;\\\Oracle宣布了2017年新接納的Java Champion的綜述。這次宣布了40位新的成員&#xff0c;包括InfoQ的貢獻者Monica Beckwith。…

lambda ::_您無法從這里到達那里:Netlify Lambda和Firebase如何使我陷入無服務器的死胡同

lambda ::[Update: Apparently you can get there from here! That is, if you use firebase-admin instead of google-cloud/firestore. Ill have more on this in the future, but the gist of it is summarized here.][ 更新&#xff1a;顯然您可以從這里到達那里&#xff…

leetcode 264. 丑數 II(堆)

給你一個整數 n &#xff0c;請你找出并返回第 n 個 丑數 。 丑數 就是只包含質因數 2、3 和/或 5 的正整數。 示例 1&#xff1a; 輸入&#xff1a;n 10 輸出&#xff1a;12 解釋&#xff1a;[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 個丑數組成的序列。 解題思路 維…

奇跡網站可視化排行榜]_外觀可視化奇跡

奇跡網站可視化排行榜]When reading a visualization is what we see really what we get?閱讀可視化內容時&#xff0c;我們真正看到的是什么&#xff1f; This post summarizes and accompanies our paper “Surfacing Visualization Mirages” that was presented at CHI …

Oracle自動性能統計

Oracle自動性能統計 高效診斷性能問題&#xff0c;需要提供完整可用的統計信息&#xff0c;好比醫生給病人看病的望聞問切&#xff0c;才能夠正確的確診&#xff0c;然后再開出相應的藥方。Oracle數據庫為系統、會話以及單獨的sql語句生成多種類型的累積統計信息。本文主要描述…