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. Data transfer from GPIO port to RAM buffer using DMA upon receiving a trigger signal on the timer capture input channel.

    Data transfer from GPIO port to RAM buffer using DMA upon receiving a trigger signal on the timer ca ...

  2. 使用cwRsync实现windows下文件定时同步

    1.参考文献: 使用cwRsync实现windows下文件定时同步(备份) 文件同步工具CwRsync的使用方法及常用命令详解 2.背景: 当前的SCADA架构中,有1台Server,5台FE,还有1 ...

  3. Golang 特性简介

    by sheepbao 主要大概介绍go语言的历史和特性,简单的入门. 来历 很久以前,有一个IT公司,这公司有个传统,允许员工拥有20%自由时间来开发实验性项目.在2007的某一天,公司的几个大牛, ...

  4. golang-bcc-bpf-function-tracing

    http://www.brendangregg.com/blog/2017-01-31/golang-bcc-bpf-function-tracing.html

  5. MySQL编码latin1转utf8

    mysql移植含有中文的数据时,很容易出现乱码问题.很多是在从mysql4.x向mysql5.x移植的时候出现.mysql的缺省字符集是 latin1,在使用mysql4.x的时候,很多人都是用的la ...

  6. error: internal error: unable to execute QEMU command &#39;migrate&#39;: this feature or command is not cur

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正. 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  7. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  8. tms mqtt

    tms mqtt 功能概述 MQTT客户端组件 可用于VCL,FMX和LCL应用 支持Windows,iOS,Android,macOS,Linux,Raspberry Pi 实现完整的MQTT规范, ...

  9. OpenLdap+MySQL笔记

    20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...

  10. Linux内核:分析coredump文件 - 内核代码崩溃

    转自:http://blog.csdn.net/guowenyan001/article/details/12975221 一.分析Core文件 1.1 找到core文件目录,启动mycrash:my ...