当前位置:网站首页>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];
}
边栏推荐
- 使用HBuilder X编辑器安装终端窗口插件未响应的解决方案
- Good things to share
- Programmers' choice of taking private jobs and part-time jobs
- 使用smart-doc自动生成接口文档
- Learning the interpretable representation of quantum entanglement, the depth generation model can be directly applied to other physical systems
- 防抖&节流 加强版
- After passing the hcip exam, I still failed to change my career. What do professional network workers value most
- three.js模拟驾驶游览艺术展厅---打造超级相机控制器
- OJ每日一练——跨越2020
- SOA Service Oriented Architecture
猜你喜欢

考过HCIP依然转行失败,职业网工最看重的到底是什么

c语言---17 函数简介

异步FIFO

wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz

Ensure database and cache consistency

SourceTree版本管理常用操作

数据库访问工具简介

mysql主从同步及其分库分表基本流程

Common operations of sourcetree version management

Future alternatives to IPv4! Read the advantages, features and address types of IPv6
随机推荐
剑指 Offer 11. 旋转数组的最小数字
The breakthrough of key chips hindering Huawei 5g mobile phones has been achieved, and domestic chips have gained 10% share
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
Digital data depth | about software self-control, source code left, no code right
OJ每日一练——跨越2020
数据库访问工具简介
Tianyi cloud takes advantage of the new infrastructure to build a "4+2" capability system for digital transformation
SourceTree版本管理常用操作
Customize multi-level list styles in word
Smart data won two annual awards at the second isig China Industrial Intelligence Conference
js判断浏览器是否打开了控制台
2021-04-05
OJ每日一练——整理命名
Tp5.1 solving cross domain problems
Reverse proxy haproxy
优化——线性规划
程序员接私活兼职选择
多种方案实现图片的懒加载
Use smart doc to automatically generate interface documents