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. 递归


  1. 迭代


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



xgqfrms 2012-2020

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


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

  1. [LeetCode] 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: Binary Search Tree Iterator 解题报告

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

  4. [LeetCode] 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. [Leetcode] Binary search -- 475. Heaters

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

  7. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

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

  8. [Leetcode] Binary search, Divide and conquer--240. 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. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

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

随机推荐

  1. 初识JavaScript和jQuery

    JavaScript 1.特性 ①脚本语言.JavaScript是一种解释型的脚本语言,C.C++.Java等语言先编译后执行, 而JavaScript是在程序的运行过程中逐行进行解释. ②基于对象. ...

  2. VMware vSphere (EXSI) 安装使用

    VMware vSphere 镜像下载 VMware vSphere Hypervisor (ESXi) 6.7 https://my.vmware.com/cn/web/vmware/downloa ...

  3. URL 重定向机制

    由于存在上述三种 URL 重定向机制,那么在多种方法同时设定的情况下,哪种方法会首先起作用呢?优先级顺序如下: HTTP 协议的重定向机制永远最先触发,即便是在没有传送任何页面--也就没有页面被(客户 ...

  4. Redis 雪崩、穿透和击穿

    https://github.com/doocs/advanced-java/blob/master/docs/high-concurrency/redis-caching-avalanche-and ...

  5. CF413C

    正文 题意: 给 n 个关卡,每个关卡得分为 ai,有 m 次机会可以选择一 个关卡通过后不得分,而将现有得分翻倍 你可以安排关卡的通过顺序和策略,求最大得分. 分析: 看到这道题首先想到的就是贪心, ...

  6. HDU3062 PARTY

    Party Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛 ...

  7. python基础三---- time模块,函数的定义和调用

    此处重点说明一下: 注意: 1.用例之间不要存在依赖关系,每个用例都可以单独运行 2.用例不要互相调用,需要调用的公共方法可以写成方法去调用 1.等待 (在脚本运行的时候,有些线程之间需要间隔时间,可 ...

  8. redis性能优化、内存分析及优化

    redis性能优化.内存分析及优化 1.优化网络延时 2.警惕执行时间长的操作 3.优化数据结构.使用正确的算法 4.考虑操作系统和硬件是否影响性能 5.考虑持久化带来的开销 5.1 RDB 全量持久 ...

  9. Spark程序使用Scala进行单元测试

    Spark程序使用Scala进行单元测试 1.Rdd测试 2.无返回值方法测试 3.测试私有方法 原文作者:大葱拌豆腐 原文地址:Spark程序进行单元测试-使用scala 1.Rdd测试 spark ...

  10. Inceptor常用SQL

    1.创建数据库 建一个数据库exchange_platform. DROP DATABASE IF EXISTS exchange_platform CASCADE; CREATE DATABASE ...