当前位置:网站首页>Find minimum in rotated sorted array
Find minimum in rotated sorted array
2022-06-23 09:35:00 【ruochen】
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
The sequence is basically in order , Then use binary search . Suppose the sequence is n,s It's the starting subscript ,e Is the last subscript ,m Is the middle element subscript . There are three situations :
ns < ne
nm > ns > ne
nm < ne < ns
situation 1:ns < ne
0 1 2 4 5 6 7
This situation , Go straight back to ns. Because the sequence is ordered , Or just rotate once . If ns < ne, Indicates that there is no rotation . The smallest element is ns.
situation 2:nm > ns > ne
4 5 6 7 0 1 2
public int findMin(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
if (nums.length == 2) {
return Math.min(nums[0], nums[1]);
}
int s = 0, e = nums.length - 1;
int m = (s + e) / 2;
if (nums[s] < nums[e]) {
return nums[s];
}
if (nums[m] > nums[s]) {
return findMin(Arrays.copyOfRange(nums, m + 1, e + 1));
}
return findMin(Arrays.copyOfRange(nums, s, m + 1));
}边栏推荐
- JSP getting started summary
- Redis learning notes - data type: Set
- 玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
- Combination sum III of leetcode topic analysis
- UEFI source code learning 3.7 - norflashdxe
- 一元函数求极限三大方法---洛必达法则,泰勒公式
- Redis learning notes - data type: ordered set (Zset)
- 栈(Stack)的链式实现详解----线性结构
- [SUCTF 2019]CheckIn
- [GYCTF2020]Blacklist
猜你喜欢
随机推荐
微信小程序:点击按钮频繁切换,重叠自定义markers,但是值不改变
swagger UI :%E2%80%8B
[SUCTF 2019]CheckIn
UCOSII (learning notes)
Getting started with cookies and sessions
Redis学习笔记—持久化机制之AOF
Redis学习笔记—单个键管理
Notes on using the coding code base
Redis learning notes - detailed explanation of redis benchmark
Redis learning notes - transactions
多线程习题
swagger UI :%E2%80%8B
嵌入式系统概述(学习笔记)
[GYCTF2020]Blacklist
太阳塔科技招聘PostgreSQL数据库工程师
Chain implementation of stack -- linear structure
Embedded system overview (learning notes)
Cesium loading orthophoto scheme
Redis学习笔记—主从复制
Lua的基本使用
![[MRCTF2020]Ez_bypass](/img/cd/bd6fe5dfc3f1942a9959a9dab9e7e0.png)



![[GYCTF2020]Blacklist](/img/a8/0f7231e5c498e6c89ff2e3bcb122f2.png)

