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, ...
随机推荐
-
matlab画图函数plot()/set/legend
简单plot()/legend/XY轴范围axis 除了坐标轴信息外还可以添加其它的信息,如所画曲线的信息等:测试代码如下 x=0:pi/20:2*pi; y1=sin(x); y2=cos(x); ...
-
php中计算二维数组中某一元素之和
[0] => array(5) { ["id"] => string(2) "11" ["name"] => string ...
-
z-index深入理解
[CSS深入理解之z-index]听课总结 (http://www.imooc.com/learn/643) 一.z-index基础知识 1.z-index的含义 z-index属性指定了元素及其 ...
-
第8章 NAND FLASH控制器
8.1 NAND Flash介绍和NAND Flash控制器使用 NAND Flash在嵌入式系统中的地位与PC上的硬盘类似 NAND Flash在掉电后仍可保存 8.1.1 Flash介绍 有NOR ...
-
delphi 调用百度地图WEBSERVICE转换GPS坐标 转
http://www.cnblogs.com/happyhills/p/3789864.html 百度地图的API说明 使用方法 第一步,申请密钥(ak),作为访问服务的依据: 第二步,按照请求参 ...
-
NodeJs获取函数名称和函数操作整理
var aa = function () { log("xxxx"); }; aa(); var model = {}; model.test = function () { lo ...
-
玩转Log4Net
玩转Log4Net 下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的 log4net-1.2.11 ...
-
获取node异步执行结果的方式
拿数据库操作举例: var connection = mysql.createConnection(); connection.query(sql,function(err,rows){xxx} ); ...
-
BZOJ1503 [NOI2004]郁闷的出纳员 splay
原文链接http://www.cnblogs.com/zhouzhendong/p/8086240.html 题目传送门 - BZOJ1503 题意概括 如果某一个员工的工资低于了min,那么,他会立 ...
-
Spring之IOC容器
在前面博客中介绍什么是依赖注入时有提到:依赖注入是组件之间依赖关系由容器在运行期决定,即由容器动态的将某个依赖关系注入到组件之中.那什么是容器?既然Spring框架实现了IOC,那Spring中的容器 ...