LeetCode & Binary Search 解题模版

时间:2022-10-11 23:45:33

LeetCode & Binary Search 解题模版

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array.

在计算机科学中,二分搜索(也称为半间隔搜索,对数搜索或二进制印章)是一种搜索算法,用于查找排序数组中目标值的位置。

Big O

Worst complexity: O(log n)

Average complexity: O(log n)

Best complexity: O(1)

Space complexity: O(1)

Data structure: Array

Class: Search algorithm

solutions

  1. 递归

LeetCode & Binary Search 解题模版


  1. 迭代

LeetCode & Binary Search 解题模版


leeetcode & binary-search

https://leetcode.com/problems/binary-search/


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-30
* @modified
*
* @description 704. Binary Search
* @difficulty Easy
* @complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/binary-search/
* @solutions
*
*/ const log = console.log; /* Example 1: Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4 Example 2: Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1 */ /**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
// left + 差值
let mid = left + Math.floor((right - left) / 2);
// log(`mid`, nums[mid])
if(nums[mid] === target) {
// nums[mid] 值
return nums.indexOf(nums[mid]);
// return true;
} else if(nums[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
// return false;
}; const test = search([-1,0,3,5,9,12], 9);
const test1 = search([-1,0,3,5,9,12], 2); log(test)
log(test1)
// 4
// -1

best solution

https://leetcode.com/submissions/detail/374279676/

runtime


/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
if(!nums.length) return -1
let start = 0, end = nums.length - 1
while(start <= end) {
if(nums[start] === target) return start
if(nums[end] === target) return end
let mid = Math.floor(start + (end - start) / 2)
if(nums[mid] === target) return mid
if(target > nums[mid]) {
start = mid + 1
} else {
end = mid - 1
}
}
return -1
};

Memory


/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let low = 0;
let high = nums.length -1;
while (low <= high) {
const mid = parseInt((low + high) / 2);
if (nums[mid] === target) {
return mid;
}
if (nums[mid] < target) {
low = mid + 1;
}
if (nums[mid] > target) {
high = mid - 1;
}
}
return -1;
}

refs

https://en.wikipedia.org/wiki/Binary_search_algorithm

https://www.youtube.com/results?search_query=binary+search+algorithm

https://www.youtube.com/watch?v=P3YID7liBug


LeetCode & Binary Search 解题模版

xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


LeetCode & Binary Search 解题模版的更多相关文章

  1. &lbrack;LeetCode&rsqb; Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  2. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

  3. LeetCode&colon; Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  4. &lbrack;LeetCode&rsqb; Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  6. &lbrack;Leetcode&rsqb; Binary search -- 475&period; Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  7. 153&period; Find Minimum in Rotated Sorted Array&lpar;leetcode&comma; binary search&rpar;

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  8. &lbrack;Leetcode&rsqb; Binary search&comma; Divide and conquer--240&period; Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. &lbrack;Leetcode&rsqb; Binary search&comma; DP--300&period; Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. matlab画图函数plot&lpar;&rpar;&sol;set&sol;legend

    简单plot()/legend/XY轴范围axis 除了坐标轴信息外还可以添加其它的信息,如所画曲线的信息等:测试代码如下 x=0:pi/20:2*pi; y1=sin(x); y2=cos(x); ...

  2. php中计算二维数组中某一元素之和

    [0] => array(5) { ["id"] => string(2) "11" ["name"] => string ...

  3. z-index深入理解

    [CSS深入理解之z-index]听课总结 (http://www.imooc.com/learn/643)   一.z-index基础知识 1.z-index的含义 z-index属性指定了元素及其 ...

  4. 第8章 NAND FLASH控制器

    8.1 NAND Flash介绍和NAND Flash控制器使用 NAND Flash在嵌入式系统中的地位与PC上的硬盘类似 NAND Flash在掉电后仍可保存 8.1.1 Flash介绍 有NOR ...

  5. delphi 调用百度地图WEBSERVICE转换GPS坐标 转

    http://www.cnblogs.com/happyhills/p/3789864.html   百度地图的API说明 使用方法 第一步,申请密钥(ak),作为访问服务的依据: 第二步,按照请求参 ...

  6. NodeJs获取函数名称和函数操作整理

    var aa = function () { log("xxxx"); }; aa(); var model = {}; model.test = function () { lo ...

  7. 玩转Log4Net

    玩转Log4Net 下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的  log4net-1.2.11 ...

  8. 获取node异步执行结果的方式

    拿数据库操作举例: var connection = mysql.createConnection(); connection.query(sql,function(err,rows){xxx} ); ...

  9. BZOJ1503 &lbrack;NOI2004&rsqb;郁闷的出纳员 splay

    原文链接http://www.cnblogs.com/zhouzhendong/p/8086240.html 题目传送门 - BZOJ1503 题意概括 如果某一个员工的工资低于了min,那么,他会立 ...

  10. Spring之IOC容器

    在前面博客中介绍什么是依赖注入时有提到:依赖注入是组件之间依赖关系由容器在运行期决定,即由容器动态的将某个依赖关系注入到组件之中.那什么是容器?既然Spring框架实现了IOC,那Spring中的容器 ...