当前位置:网站首页>Sword finger offer 11 Minimum number of rotation array
Sword finger offer 11 Minimum number of rotation array
2022-06-22 23:39:00 【Front end plasterer】
Move the first elements of an array to the end of the array , We call it rotation of arrays . Enter a rotation of an incrementally sorted array , Output the smallest element of the rotation array . for example , Array [3,4,5,1,2] by [1,2,3,4,5] A rotation of , The minimum value of the array is 1.
This problem is a simple sorting problem , stay js There are many sorting methods in
Method 1:sort Sort
function minArray(n){
return n.sort((a,b)=> a - b )[0]
}
Method 2:Math.min Sort
function minArray(n){
return Math.min(...n);
}
Method 3: Double pointer
analysis : Compare from both sides of the array to the middle , Find the smallest one in the array
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
}
Method 4: Dichotomy
analysis : Compare the size of the first two numbers and the middle number by comparison , Then cycle through the comparison , Finally determine the minimum number in the array
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];
}
边栏推荐
- 在Word中自定义多级列表样式
- [redisson] source code analysis of multilock
- 数据库访问工具简介
- Spark SQL accessing JSON and JDBC data sources
- Asynchronous FIFO
- Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction
- 斐波那契数列合集
- C language -- 17 function introduction
- OJ每日一练——删除单词后缀
- uniapp 修改数组属性,视图不更新
猜你喜欢

口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?

10 Super VIM plug-ins, I can't put them down

C language greedy snake

OJ每日一练——过滤多余的空格

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

PHP7.3报错undefined function simplexml_load_string()

Reverse proxy haproxy

Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction

2021-08-21

2021-05-02
随机推荐
mysql主从同步及其分库分表基本流程
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
Longest word in output string
OJ daily practice - filter extra spaces
three.js模拟驾驶游览艺术展厅---打造超级相机控制器
What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
Dml:data manipulation language
获取鼠标移动的方向
Unity: use ray to detect objects
防抖&节流 加强版
反向代理HAProxy
2021-08-22
Enjoy high-performance computing! Here comes the Tianyi cloud HPC solution
【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片
2021-05-02
c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试
在Word中自定义多级列表样式
js图片分辨率压缩
异步FIFO
10 Super VIM plug-ins, I can't put them down