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. Implementation of Serial Wire JTAG flash programming in ARM Cortex M3 Processors

    Implementation of Serial Wire JTAG flash programming in ARM Cortex M3 Processors The goal of the pro ...

  2. 打印 Go 结构体(struct)信息:fmt.Printf("%+v", user)

    package main import "fmt" // 用户 type User struct { Id int Name string Age int } func main( ...

  3. 面试题07_用两个栈实现队列——剑指offer系列

    题目描写叙述: 用两个栈实现一个队列. 队列的声明例如以下,请实现它的两个函数appendTail 和 deleteHead.分别完毕在队列尾部插入结点和在队列头部删除结点的功能. 解题思路: 栈的特 ...

  4. Android:活动的简单使用

    2.1    活动是什么 活动(Activity)是最容易吸引到用户的地方了,它是一种可以包含用户界面的组件, 主要用于和用户进行交互.一个应用程序中可以包含零个或多个活动,但不包含任何活动的 应用程 ...

  5. SQLCE使用

    Windows Phone的本地数据库SQL Server CE是7.1版本即芒果更新的新特性,所以你要在应用程序中使用SQL Server CE数据库必须使用Windows Phone 7.1的AP ...

  6. C#编程(四十七)----------集合接口和类型

    原文链接: http://blog.csdn.net/shanyongxu/article/details/47005979 集合接口和类型 前面介绍了数组和Array类实现的接口.数组的大小是固定的 ...

  7. 【k8s】搭建步骤

    搭建步骤 基础概念:https://www.cnblogs.com/sxdcgaq8080/p/10640879.html ====================================== ...

  8. Kettle优化就这么多

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/ClamReason/article/details/49930479 Kettle正常转换速度 场景 ...

  9. HTML5文件上传qq、百度、taobao等比较(改进支持三种状态提示)

    拖拽过程详解: 1:文件未拖出文件选择框的时候提示:将要上传的文件或文件夹拖拽至此区域 2:文件拖出文件选择框但未拖入上传的文件框提示:请继续拖拽文件或文件夹至此区域 3:文件拖出文件选择框且已拖入上 ...

  10. SharePoint 2016 安装 Cumulative Update for Service Bus 1.0 (KB2799752)报错

    前言 SharePoint 服务器场安装workflow manager 1.0的时候,报下面的错误,搜了很多博客都没有解决.然后,灵机一动,下载了一个英文版的累计更新包,安装成功了. SharePo ...