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. status 404 reading EduClient#getCourseInfoOrder(String)解决过程

    UcenterClient#getUserInfoOrder(String) failed and no fallback available.解决过程 报错内容: com.netflix.hystr ...

  2. PHP 框架之一Laravel

    Laravel: Laravel The phpFramework for Web Artisans and one of the best php framework in year 2014. L ...

  3. 丢包 ICMP

    小结: 1.ICMP 常见网络丢包故障分析及处理 云极安 云极安 2019-12-25 我们在管理维护网络的过程中经常会遇到数据包丢失的现象.使用Ping命令进行连通性测试,则会发现Ping包延时远远 ...

  4. sql 括号

    <select id="chlTransQueryByChlType" parameterType="map" resultType="java ...

  5. 机器学习基础——规则化(Regularization)

    在机器学习中,我们一直期望学习一个泛化能力(generalization)强的函数只有泛化能力强的模型才能很好地适用于整个样本空间,才能在新的样本点上表现良好. \[y=a+bx+cx^2+dx^3\ ...

  6. 非Windows系统 如何解压带中文密码和中文文件名的zip压缩文件

    数据科学交流群,群号:189158789 ,欢迎各位对数据科学感兴趣的小伙伴的加入! 一.安装unar软件包: Linux(Debian系列): apt install unarLinux(RedHa ...

  7. 项目总结—校园办公管理系统(SSM框架搭建)

    文章目录 CSDN下载地址:校园管理系统 GIT下载地址:校园管理系统 学以致用,学习完SSM框架之后,独立完成一个小院办公管理系统,熟悉框架的开发流程,熟悉项目的开发流程,完成一个简单的校园办公管理 ...

  8. Go语言学习笔记(4)——并发编程

    Golang在语言级别支持了协程,由runtime进行管理. 在Golang中并发执行某个函数非常简单: func Add(x, y int) { fmt.Println(x + y) } func ...

  9. 周期性清除Spark Streaming流状态的方法

    在Spark Streaming程序中,若需要使用有状态的流来统计一些累积性的指标,比如各个商品的PV.简单的代码描述如下,使用mapWithState()算子: val productPvStrea ...

  10. 纯js添加类

    1.el.setAttribute('class','abc'); <!DOCTYPE HTML><HTML><HEAD><meta charset=&quo ...