Binary Search Algorithm All In One
Binary Search Algorithm All In One
二分搜索算法
https://www.geeksforgeeks.org/complexity-analysis-of-binary-search/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-22
*
* @description binary search
* @augments
* @example
* @link
*
*/
let log = console.log;
// 2.写一个函数,对于一个排好序的数组,如果当中有两个数的和为某个给定的数target,返回true,否则false,时间复杂度O(n)
// supplement
const binarySearch = (arr = [], target, debug = false) => {
let result = false;
let temp = [];
for (let i = 0; i < arr.length; i++) {
let supplement = ``;
supplement = target > arr[i] ? target - arr[i] : arr[i] - target;
if (debug) {
log(`supplement =`, supplement);
}
if (arr.includes(supplement)) {
if (!temp.includes(supplement)) {
temp.push(supplement);
}
}
}
if (temp.length >= 2) {
result = true;
}
if (debug) {
log(`temp =`, temp);
}
return result;
};
log(binarySearch([1,3,5,7,9], 9));
// false
log(binarySearch([1,3,5,7,9], 4));
// true
log(binarySearch([-1,3,5,7,9], 4));
// true
167.
两数之和 II - 输入有序数组
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/
Big O
refs
https://en.wikipedia.org/wiki/Binary_search_algorithm
https://replit.com/@xgqfrms/PPLabs-frontend-answers
blogs
https://www.quora.com/What-is-the-time-complexity-of-binary-search
https://hackernoon.com/what-does-the-time-complexity-o-log-n-actually-mean-45f94bb5bfbf
https://www.geeksforgeeks.org/complexity-analysis-of-binary-search/
https://my.oschina.net/u/2822116/blog/795225
https://blog.csdn.net/ns_code/article/details/24933341
https://hellozhaozheng.github.io/z_post/面试-算法刷题-剑指offer/#42-和为S的两个数字
https://www.jianshu.com/p/e98eb999328b
https://www.cnblogs.com/DOMLX/p/10940636.html
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11392498.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2018-08-22 CSP & CORS
2016-08-22 reStructuredText(.rst) && read the docs
2016-08-22 Selenium 前端自动化工具
2016-08-22 CSS3 优先级