[Algorithm] Search element in a circular sorted array
function findInCircularlySortedAry (ary = [], target) {
if (ary && ary.length === ) {
return -;
} if (ary.length === ) {
return ary[] === target ? : -;
} let low = ,
high = ary.length - ; while(low <= high) {
let mid = Math.floor((low + high) / );
// case 1: target === middle item, return found
if (ary[mid] === target) {
return mid;
}
// To find which parts (left or right) is sorted
// case 2: if middle < high, mean from middle to high is sorted
if (ary[mid] < ary[high]) {
if (target > ary[mid] && target <= ary[high]) {
low = mid + ;
} else {
high = mid - ;
}
}
// case 3: if low < middle, mean from low to middle is sorted
else {
if (target >= ary[low] && target < ary[mid]) {
high = mid -;
} else {
low = mid + ;
}
}
} return -;
} const data = [,,,,,,,];
const res = findInCircularlySortedAry(data,); // 2
console.log(res);
We don't need to
[Algorithm] Search element in a circular sorted array的更多相关文章
- [Algorithm] How many times is a sorted array rotated?
Given a sorted array, for example: // [2,5,6,8,11,12,15,18] Then we rotated it 1 time, it becomes: / ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- 33.[LeetCode] Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
随机推荐
- Go Session 使用简介
6.session和数据存储 6.1 session和cookie 6.2 Go如何使用session 6.3 session存储 6.4 预防session劫持 6.5 小结
- MySQL Workbench的安全更新模式
MySQL Workbench上使用"DELETE FROM TABLE_E;"清空一个表时返回错误: Error Code: 1175. You are using safe u ...
- mysql 和 sqlite 区别 及 SQLite Expert Professional sqliteManager 区别
mysql 和 sqlite 区别 SQLITE是单机数据库.功能简约,小型化,追求最大磁盘效率MYSQL是完善的服务器数据库.功能全面,综合化,追求最大并发效率 MYSQL.Sybase.Oracl ...
- 升级WINDOWS10后任务栏的图标老是闪动是怎么回事
解决方法:1.进入设置→更新和安全→恢复2.找到高级启动,点击“立即重启3.重启后,进入第一个选择画面,点击“疑难解答”4.然后点击“高级选项”5.在其中选择“启动设置”6.这里给出了下次重启后的主要 ...
- Android提交数据到服务器的两种方式四种方法
本帖最后由 yanghe123 于 2012-6-7 09:58 编辑 Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方 ...
- SpringBoot扫描包提示找不到mapper的问题
SpringBoot扫描包问题 报错信息:Consider defining a bean of type in your configuration 方法一: 使用注解 @ComponentScan ...
- 都市侠盗第一季/全集Leverage迅雷下载
第一季 Leverage Season 1 (2008)看点:Nate Ford(蒂莫西·赫顿 Timothy Hutton 饰)曾当过保险调查员,为自己的老板挽回过数百万美元的损失,是个忠心耿耿的雇 ...
- 实习医生风云第一至九季/全集Scrubs迅雷下载
本季看点:<实习医生风云>一批医学院的学生来到圣心医院开始他们的实习生涯,但是从第一天起就发现这里并不是想象中安详宁静的医学圣地,从医生到护士甚至门卫个个不同寻常.内科实习医生杰迪是个聪明 ...
- unity 打包资源及网络请求资源包
第一步 导包 在Assets新建一个Editor目录 新建一个Test类 using UnityEngine; using System.Collections; using UnityEditor; ...
- Notification详解(含工具类)
昨天一天只写了两篇文章,效率超低.追其原因呢,其实我一直在研究noti ...