当前位置:网站首页>剑指 Offer 11. 旋转数组的最小数字-二分查找
剑指 Offer 11. 旋转数组的最小数字-二分查找
2022-06-13 04:06:00 【hequnwang10】
一、题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一次旋转,该数组的最小值为 1。
注意,数组 [a[0], a[1], a[2], …, a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], …, a[n-2]] 。
示例 1:
输入:numbers = [3,4,5,1,2]
输出:1
示例 2:
输入:numbers = [2,2,2,0,1]
输出:0
二、解题
二分查找
这题有重复数字,分为三种情况
中值==右值 :right -= 1;
中值 < 右值: right = mid;
中值 > 右值 :left = mid + 1;
class Solution {
public int minArray(int[] numbers) {
//这题是包含重复的数字,所以分为三种情况
int left = 0,right = numbers.length - 1;
while(left < right){
int mid = left+(right - left) / 2;
if(numbers[mid] < numbers[right]){
right = mid;
}else if(numbers[mid] > numbers[right]){
left = mid + 1;
}else{
right -= 1;
}
}
return numbers[left];
}
}
时间复杂度:O(log(n);
空间复杂度:O(1)。
边栏推荐
- 单片机信号发生器程序
- 5G China unicom AP:B SMS ASCII 转码要求
- 双目视觉——打造室外避障的“最优解”
- LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC
- Unity shader learning 004 shader debugging platform difference third-party debugging tools
- GoFrame第四天
- ET框架-22 创建ServerInfo实体及事件
- Zoom and move the H5 part of the mobile end
- 单片机:PCF8591硬件接口
- Goframe day 5
猜你喜欢

Single chip microcomputer: main index of a/d (analog-to-digital conversion)

Alipay native components (hotel time selection)

Data analysis report

Call C function in Lua

MCU: pcf8591 hardware interface

On the value of line height
![[web] cookies and sessions](/img/00/b10a9f4ae918afe5d032a9af20a49c.png)
[web] cookies and sessions

R: Employee turnover forecast practice

Intervention analysis + pseudo regression

Goframe day 5
随机推荐
单片机串口通信原理和控制程序
GoFrame第五天
单片机:EEPROM介绍与操作
[web] cookies and sessions
基于PHP的轻量数码商城系统
Introduction and use of ES6
单片机:EEPROM 多字节读写操作时序
Et framework -22 creating serverinfo entities and events
Single chip microcomputer: basic concepts of a/d and d/a
leetcode. 1 --- sum of two numbers
MSG messages in ROS
[zeloengine] localization process /imgui Chinese culture
R: Airline customer value analysis practice
Difference between OKR and KPI
高等数学(第七版)同济大学 习题1-3 个人解答
单片机:PCF8591硬件接口
LVS四層負載均衡集群(3)集群功能分類 - HPC
El expression
Configuration and practice of shardingsphere JDBC sub database separation of read and write
Cache read / write -- write