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的更多相关文章

  1. [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: / ...

  2. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  3. 【题解】【数组】【查找】【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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  8. 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 ...

  9. 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. ...

随机推荐

  1. STM32CubeF4 FreeRTOS Examples don't work correctly with HAL_GetTick

    because the SysTick ISR has been assigned to the FreeRTOS xPortSysTickHandler() function without reg ...

  2. IIS7.0下 HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求

    IIS7.0下 HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求   IIS7.0下查询条件太多时,会报错,因为IIS 7对于Query Str ...

  3. Memcache 分布式高可用集群介绍

    分布式缓存需考虑如下三点: 1.缓存本身的水平线性扩展的问题. 2.缓存大病罚下的本身性能问题. 3.避免缓存的单点鼓掌问题. 分布式缓存存在的问题: 1.内存本身的管理问题.内存的分配,管理和回收机 ...

  4. C++ STL set::find的用法

      参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - opera ...

  5. 大不列颠百科全书Encyclopaedia Britannica Ultimate 2014光盘镜像

    大不列颠百科全书又名大英百科全书,是目前最古老的百科全书之一.大英百科全书每10余年出一个版本,如今已经推出到Encyclopaedia Britannica Ultimate 2014.此次推荐的是 ...

  6. Delphi XE中String、ANSIString、TBytes之间的转换

    一.string转为ansistring1.直接赋值 (有警告)2.ansistring()类型强制转换.(无警告) 二.ansistring 转为string 1.直接赋值 (有警告)2.strin ...

  7. Selenium:Hello,World!

    背景 伟鹏同学在学习自动化测试了,开发人员也有必要学习一下,有如下好处: 可以开发一些小工具. 可以熟悉一下自动化测试开发技术. 代码 using System; using Microsoft.Vi ...

  8. 再谈Linux内核中的RCU机制

    转自:http://blog.chinaunix.net/uid-23769728-id-3080134.html RCU的设计思想比较明确,通过新老指针替换的方式来实现免锁方式的共享保护.但是具体到 ...

  9. Winform窗体传值

    1:委托: 父窗体; private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); //frm.f ...

  10. Redis 性能问题的记录

    最近线上使用redis, 查询的情况不甚理想, 这个查询操作是个 lua 脚本, 包含如下操作 开发机 redis, 没有其他干扰, 插入的 zset 有 5000 member 左右, 使用的 re ...