当前位置:网站首页>LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
2022-07-07 13:22:00 【Old street of small town】
1. subject
We know a length of n Array of , In ascending order in advance , Through 1 To n Time After rotation , Get the input array . for example , Original array nums = [0,1,2,4,5,6,7] After the change may get :
If you rotate 4 Time , You can get [4,5,6,7,0,1,2]
If you rotate 7 Time , You can get [0,1,2,4,5,6,7]
Be careful , Array [a[0], a[1], a[2], …, a[n-1]] Rotate once The result is an array [a[n-1], a[0], a[1], a[2], …, a[n-2]] .
Give you an element value Different from each other Array of nums , It turns out to be an ascending array , According to the above situation, we have made many rotations . Please find and return the smallest element in the array .
You have to design a time complexity of O(log n) The algorithm to solve this problem .
Example 1:
Input :nums = [3,4,5,1,2]
Output :1
explain : The original array is [1,2,3,4,5] , rotate 3 Get the input array for the first time .
Example 2:
Input :nums = [4,5,6,7,0,1,2]
Output :0
explain : The original array is [0,1,2,4,5,6,7] , rotate 4 Get the input array for the first time .
Example 3:
Input :nums = [11,13,15,17]
Output :11
explain : The original array is [11,13,15,17] , rotate 4 Get the input array for the first time .
Tips :
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
nums All integers in are different from each other
nums It turns out to be an ascending array , And carried on 1 to n Second rotation
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array
2. Ideas
(1) Two point search
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— Two point search
class Solution {
public static int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
// If nums[left] < nums[right], shows nums[left...right] The elements between are in ascending order , So the smallest element is nums[left]
if (nums[left] < nums[right]) {
return nums[left];
}
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[right];
}
}
边栏推荐
- Cmake learning and use notes (1)
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- The difference between cache and buffer
- Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- ESP32 ① 编译环境
- Why can basic data types call methods in JS
- MongoDB 分片总结
- PAcP learning note 3: pcap method description
- LIS 最长上升子序列问题(动态规划、贪心+二分)
猜你喜欢

线程池拒绝策略最佳实践

Analysis of DHCP dynamic host setting protocol

靠卖概念上市,认养一头牛能走多远?

Sequoia China completed the new phase of $9billion fund raising
![Scripy tutorial classic practice [New Concept English]](/img/bc/f1ef8b6de6bfb6afcdfb0d45541c72.png)
Scripy tutorial classic practice [New Concept English]

Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images

PACP学习笔记一:使用 PCAP 编程

Digital IC Design SPI

Go language learning notes - structure

如何让join跑得更快?
随机推荐
Mongodb slice summary
Cmake learning and use notes (1)
Read PG in data warehouse in one article_ stat
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
数字ic设计——SPI
如何让join跑得更快?
error LNK2019: 无法解析的外部符号
Differences between MySQL storage engine MyISAM and InnoDB
MongoDB复制(副本集)总结
Practical case: using MYCAT to realize read-write separation of MySQL
PACP学习笔记一:使用 PCAP 编程
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Grep of three swordsmen in text processing
How did Guotai Junan Securities open an account? Is it safe to open an account?
MongoDB命令汇总
[learning notes] segment tree selection
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
User management summary of mongodb