LeetCode & Binary Search 解题模版
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
- 递归
- 迭代
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 解题模版的更多相关文章
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- [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 ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- 京东热 key 探测框架新版发布,单机 QPS 可达 35 万
https://mp.weixin.qq.com/s/3URAvUF6zwxeF5Kkc1aWHA 京东热 key 探测框架新版发布,单机 QPS 可达 35 万 原创 Hollis Hollis 2 ...
- BigDecimal add方法问题:调用add后,求和结果没变
import java.math.BigDecimal; public class DecimalAdd { public static void main(String[] args) { BigD ...
- 语言反射规则 - The Laws of Reflection
[译]Go反射的三个原则(官方博客) | seven的分享 https://sevenyu.top/2019/12/21/laws-of-reflection.html wilhg/The-Laws- ...
- SpringMVC听课笔记(三:使用@RequestMapping映射请求)
1. Spring MVC使用 @RequestMapping 注解为控制器指定可以处理哪些URL请求 2. 标注点: --类定义处:提供初步的请求映射信息.相对于WEB应用的根目录 --方法处:提供 ...
- Redis分布式锁升级版RedLock及SpringBoot实现
分布式锁概览 在多线程的环境下,为了保证一个代码块在同一时间只能由一个线程访问,Java中我们一般可以使用synchronized语法和ReetrantLock去保证,这实际上是本地锁的方式.但是现在 ...
- Java——I/O入门相关练习代码
流的概念 读取文件 读取文件1 读取文件2 读取文件3 读取文件4 skip跳过n个字节后再开始读取 读取过程中暂停给当前位置做一个标记下一次从标记位置开始读取 序列流集合流 把三个流添加到集合中合并 ...
- Maven 私服(Nexus)
@[toc](Maven 私服(Nexus)) 1.Nexus 简介 Nexus是Maven仓库管理器,也可以叫Maven的私服.Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库 ...
- 将Windows7系统改造为Linux(Centos7)系统
作为一个程序员,居然一次都没有安装过系统,果断被嘲笑了一番. 没办法,突然BOSS分配任务,将一台服务器的电脑从windos7改为Linux系统,一脸懵逼. 下面记录一下改造过程. 将Windows7 ...
- SpringMVC系列(一)核心:处理请求流程
http://blog.csdn.net/zhaolijing2012/article/details/41596803
- apache 创建多端口监听
httpd.conf 将 #LoadModule vhost_alias_module modules/mod_vhost_alias.so 改为 LoadModule vhost_alias_mod ...