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. 自监督SOTA框架 | BYOL(优雅而简洁) | 2020

    文章原创自微信公众号「机器学习炼丹术」 作者:炼丹兄 联系方式:微信cyx645016617 本篇文章主要讲解两个无监督2020年比较新比较火的论文: 论文名称:"Bootstrap You ...

  2. go mod 以及vscode解决被墙的插件问题

    https://blog.csdn.net/weixin_39003229/article/details/97638573?utm_medium=distribute.pc_relevant.non ...

  3. 解决windows git乱码问题

    在windows中打开git bash git config --global i18n.commitencoding utf-8       设置提交日志使用utf-8 git config --g ...

  4. MySQL 高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用 MySQL 保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名 ...

  5. loj10012 Best Cow Fences

    题目描述 原题来自:USACO 2003 Mar. Green 给定一个长度为 N 的非负整数序列 A ,求一个平均数最大的,长度不小于 L 的子段. 输入格式 第一行用空格分隔的两个整数 N 和 L ...

  6. Java——反射机制

    反射概念: Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称的class的内部信息,包括其modifier ...

  7. socket套接字编程(1)——基本函数

    TCP交互流程: 服务器:1. 创建socket:2. 绑定socket和端口号:3. 监听端口号:4. 接收来自客户端的连接请求:5. 从socket中读取字符:6. 关闭socket. 客户端:1 ...

  8. java格式化json串

    package com.loan.modules.extbiz.in.rabbitmq.util; import com.loan.modules.extbiz.in.rabbitmq.excepti ...

  9. linux系统权限管理拓展:特殊权限

    文件与目录权限设置不止读.写.执行这些,还有所谓的特殊权限,由于特殊权限会拥有一些"特权": 1 2 3 4 本章内容 SUID SGID SBIT 文件扩展权限ACL 1.SUI ...

  10. 交换机CPU使用率高的原因

    交换机CPU的功能 1.管理已配置的软件协议,例如: – 生成树协议(STP) – 路由协议,例如OSPF和EIGRP – 热备路由协议(HSRP) – 思科发现协议(CDP) – 端口聚合协议(PA ...