[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. ...
随机推荐
- General PE format layout
- CF330 C. Purification 认真想后就成水题了
C. Purification time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 图解tensorflow 源码分析
http://www.cnblogs.com/yao62995/p/5773578.html https://github.com/yao62995/tensorflow
- [DLX反复覆盖] hdu 3656 Fire station
题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...
- Revit API遍历系统族布置喷头
系统族可以通过内参遍历,遍历出来是个FamilySymbol喷头属于系统族,但不能通过NewDuct();类似这样的方法布置.必须使用 NewFamilyInstance() ); ...
- 源码编译Tkinter
要让Python支持Tkinter, 需要首先安装tcl和tk两个软件包. 下载地址: http://www.tcl.tk/software/tcltk/download.html 或 tcl:htt ...
- 为 JIRA 6.x 安装中文语言包
20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...
- [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
android线程池的理解,晚上在家无事 预习了一下android异步加载的例子,也学习到了一个很重要的东东 那就是线程池+缓存 下面看他们的理解. [size=1.8em]Handler+Runn ...
- SharePoint PowerShell 批量删除遗弃视图
前言 最近,给SharePoint升级了,然后发现,有一大批视图不需要了,而且,名字是一样的,想着怎么清理,然后,就想到了powershell. powershell 示例: $siteUrl = & ...
- 【转】Itunes Connect新版本如何提交应用
本文系转载,版权归原作者所有(原文链接>>). How do I submit my app to iTunes connect? To submit your app to iTunes ...