当前位置:网站首页>剑指 Offer 11. 旋转数组的最小数字
剑指 Offer 11. 旋转数组的最小数字
2022-06-22 20:51:00 【前端粉刷匠】
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个旋转,该数组的最小值为1。
这个问题就是简单的排序问题,在js中有很多排序的方法
方法1:sort排序
function minArray(n){
return n.sort((a,b)=> a - b )[0]
}
方法2:Math.min排序
function minArray(n){
return Math.min(...n);
}
方法3: 双指针
解析:从数组的两边向中间比较,找出数组中最小的那个
function minArray(n) {
let result = [0];
for (let i = 0; i < n.length; i++) {
result = Math.min(result,n[i],n[n.length - 1 - i])
}
return result
}
方法4:二分法
解析:通过比比较首尾两个数与中间的数的大小,然后循环比较,最终确定数组中的最小的数
function minArray(n) {
let left = 0;
let right = n.length - 1;
while(left < right){
let flag = Math.floor((left + right) / 2);
console.log(flag)
if(n[left] > n[flag]){
right = flag
}else if(n[right] < n[flag]){
left = flag + 1;
}else{
right --;
}
}
return n[left];
}
边栏推荐
- 2021-08-21
- node-fetch下载文件
- Summary of transport layer knowledge points
- Why do you perform performance tests before the software goes online? How to find a software performance testing organization
- Solution to cache inconsistency
- 数据库访问工具简介
- Is it bad for NFT that the market starts to cool down?
- 2021-01-29
- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
- Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
猜你喜欢

In a frame because it set 'X-FRAME-OPTIONS' to' deny '

2020-12-04

Some shaders in AB package do not trigger the callback of ipreprocessshaders

Technology cloud report: East to West computing is not only about "computing", but also needs "new storage"

2021-04-14

In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!

Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)

Explain the startup process of opengauss multithreading architecture in detail

c语言---17 函数简介

leetcode. 11 --- container with the most water
随机推荐
一个spark app demo
Greedy distribution problem (2)
Remote access and control - SSH Remote Management and TCP wrappers access control
Explain the startup process of opengauss multithreading architecture in detail
Case 2 of SQL performance degradation caused by modifying implicit parameters
MySQL functions
Three cache methods and principles
Lua-- use of data types, variables, loops, functions and operators
Phantomjs utility code snippet (under continuous update...)
In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6
In a frame because it set 'X-FRAME-OPTIONS' to' deny '
Summary of just meal with 900W increase in playback and acclaim from station B users
2021-03-06
为 localStorage 添加过期时间
2021-07-27
Mysql database design
Valid parentheses
ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”
Several soapdatamodules on Delphi soap WebService server side
Redis big key problem