LeetCode Binary Search All In One
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
- 位运算
/**
* @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;
};
- 经典双指针
/**
* @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的更多相关文章
- [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 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- [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, ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- 从源码解析Nginx对 Native aio支持_运维_youbingchen的博客-CSDN博客 https://blog.csdn.net/youbingchen/article/details/51767587
从源码解析Nginx对 Native aio支持_运维_youbingchen的博客-CSDN博客 https://blog.csdn.net/youbingchen/article/details/ ...
- Socket的用法——NIO包下SocketChannel的用法 ———————————————— 版权声明:本文为CSDN博主「茶_小哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/ycgslh/article/details/79604074
服务端代码实现如下,其中包括一个静态内部类Handler来作为处理器,处理不同的操作.注意在遍历选择键集合时,没处理完一个操作,要将该请求在集合中移除./*模拟服务端-nio-Socket实现*/pu ...
- cookie,session,token傻傻分不清
什么是认证(Authentication) • 通俗地讲就是验证当前用户的身份,证明"你是你自己"(比如:你每天上下班打卡,都需要通过指纹打卡,当你的指纹和系统里录入的指纹相匹配时 ...
- POJ1195 二维线段树
Mobile phones POJ - 1195 Suppose that the fourth generation mobile phone base stations in the Tamper ...
- js部分知识整理,google浏览器的代码调试
整理一些学过的js知识点,包括js中3个括号的含义,this的使用,递归,google浏览器的代码调试.Location的属性及常用方法,window对象常用方法,open方法等. js括号 在js中 ...
- git的几种实用操作(合并代码与暂存复原代码)
总述 git工具也用了很久,自己也写了几篇使用教程,今天继续给大家分享一些我工作中使用过的git操作. 1.git合并远程仓库的代码 2.git stash保存当前的修改 这两种情况大家应该都 ...
- CF-1445 C - Division 数论,质因数,唯一分解定理
题意 给定一个 \(p (p\le 10^{18})\), 一个 \(q(q \le 10^9)\), 要找到一个最大的 \(x\) 满足: \(p \%x = 0\) \(q \% x \neq 0 ...
- CF-311B Cats Transport(斜率优化DP)
题目链接 题目描述 小S是农场主,他养了 \(M\)只猫,雇了 \(P\) 位饲养员. 农场中有一条笔直的路,路边有 \(N\) 座山,从 \(1\) 到 \(N\)编号. 第 \(i\) 座山与第 ...
- Codeforces Round #627 (Div. 3) B - Yet Another Palindrome Problem(逻辑)
题意: 问一个数组中是否存在至少长为3的回文子数组(按下标排列,可不连续). 思路: 找三个相同数或两个不连续的相同数. #include <bits/stdc++.h> using na ...
- HDOJ 1848(SG函数)
对于SG函数来说,sg[y]=x的意义为,x与y的输赢状态是相同的 sg[y]=mex(y)的定义与n.p点的定义是相同的 #include<iostream>#include<cs ...