LeetCode Binary Search All In One

Binary Search

二分查找算法

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

https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-leetcode/

复杂度分析

时间复杂度:\mathcal{O}(\log N)O(logN)。

空间复杂度:\mathcal{O}(1)O(1)。

LeetCode Binary Search Best Solutions in JavaScript

  1. 位运算

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
const search = (nums, target) => {
if(nums.length === 0){
return -1;
}
let lo = 0;
let hi = nums.length - 1;
while(lo <= hi){
// 位运算 12 >> 1 === 6
const mid = lo + ((hi - lo) >> 1);
if(nums[mid] === target){
return mid;
}else if(nums[mid] < target){
lo = mid + 1;
}else{
hi = mid - 1;
}
}
return -1;
};
  1. 经典双指针
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let left = 0;
let right = nums.length-1
let middle
while(right >= left){
middle = Math.floor((left+right)/2)
const midval = nums[middle]
if(midval === target) return middle;
else if(midval > target) right = middle-1;
else left = middle +1;
}
return -1
};

bad

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
const findValue = (arr, target) => {
let result = -1;
let len = arr.length;
let index = Math.floor(len / 2);
let mid = arr[index];
let leftArr = arr.slice(0, index)
let rightArr = arr.slice(index + 1, len)
if(mid === target) {
result = nums.indexOf(mid);
} else {
if(mid > target) {
// left
result = findValue(leftArr, target)
}
if(mid < target) {
// right
result = findValue(rightArr, target)
}
}
return result;
}
return findValue(nums, target);
};


refs

https://leetcode.com/problemset/all/

https://leetcode-cn.com/problemset/all/




xgqfrms 2012-2020

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


LeetCode Binary Search All In One的更多相关文章

  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 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  3. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

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

  4. LeetCode Binary Search Tree Iterator

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

  5. [Leetcode] Binary search -- 475. Heaters

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

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

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

  7. [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 ...

  8. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

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

  9. LeetCode: Binary Search Tree Iterator 解题报告

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

随机推荐

  1. Spring Security 实战干货:分布式对象SharedObject

    1. 前言 在上一篇我们对AuthenticationManager的初始化的细节进行了分析,其中里面有一段代码引起了不少同学的注意: ApplicationContext context = htt ...

  2. Win2008 server R2重置登录密码Administrator

    1.PE方式修改密码 背景:https://www.cnblogs.com/Crazy-Liu/p/11245730.html 上述连接中的有AD域的机器系统使用哑巴式老毛桃等启动PE出现以下: 原因 ...

  3. https://twistedmatrix.com/documents/current/core/howto/defer.html

    https://twistedmatrix.com/documents/current/core/howto/defer.html

  4. bcprov-jdk15on包用于创建CSR(证书请求)

    <!-- Eureka注册中心客户端依赖 --> <dependency> <groupId>org.springframework.cloud</group ...

  5. 一个基于protocol buffer的RPC实现

    Protocol Buffer仅仅是提供了一套序列化和反序列化结构数据的机制,本身不具有RPC功能,但是可以基于其实现一套RPC框架. Services protocol buffer的Service ...

  6. 强连通分量 与 2-SAT

    近期一直在刷这方面的题 因为没法学新知识 但又想写点什么 就水篇博文吧 引理 简单来说,在一个有向图中,若所有点之间两两互相直接可达,则将这个图成为强连通分量 强连通分量可以是某个有向图中的子图 求强 ...

  7. LOJ10096掠夺计划

    题目传送门:https://loj.ac/problem/10096 ----------------------------------------------------------------- ...

  8. JavaScript 类型、原型与继承学习笔记

    目录 一.概览 二.数据类型 1. JavaScript中的数据类型 2. 什么是基本类型(Primitive Data Type) 2.1 概念 2.2 七个基本类型 2.3 基本类型封装对象 3. ...

  9. Spring Boot 基础,理论,简介

    Spring Boot 基础,理论,简介 1.SpringBoot自动装配 1.1 Spring装配方式 1.2 Spring @Enable 模块驱动 1.3 Spring 条件装配 2.自动装配正 ...

  10. js基础(使用Canvas画图)

    HTML5的元素提供了一组JavaScript API,让我们可以动态地创建图形和图像. 图形是在一个特定的上下文中创建的,而上下文对象目前有两种.第一种是2D上下文,可以执行原始的绘图操作, 比如: ...