LeetCode编程训练 - 折半查找(Binary Search)
Binary Search基础
应用于已排序的数据查找其中特定值,是折半查找最常的应用场景。相比线性查找(Linear Search),其时间复杂度减少到O(lgn)。算法基本框架如下:
//704. Binary Search
int search(vector<int>& nums, int target) { //nums为已排序数组
int i=,j=nums.size()-;
while(i<=j){
int mid=(i+j)/;
if(nums[mid]==target) return mid;
else if(nums[mid]>target) j=mid-;
else i=mid+;
}
return -;
}
以上查找范围的上下限 i 和 j 代表索引,算法过程可视化:Binary Search,STL中有序区间函数upper_bound/lower_bound内用的查找方法即是折半查找。
相关LeetCode题:
34. Find First and Last Position of Element in Sorted Array 题解
33. Search in Rotated Sorted Array 题解
按值范围折半查找
//287. Find the Duplicate Number
int findDuplicate(vector<int>& nums) {
int n=nums.size();
int i=,j=n-; //[i,j]表示值的区间
while(i<=j){
int mid=(i+j)/,count=;
for(auto k:nums)
if(k<=mid) ++count; //根据计数折半缩小区间if(count<=mid) i=mid+;
else j=mid-;
}
return i; //最终返回值本身
}
相关LeetCode题:
378. Kth Smallest Element in a Sorted Matrix 题解
1011. Capacity To Ship Packages Within D Days 题解
410. Split Array Largest Sum 题解
求递增序列(LIS, longest increasing subsequence)是一道经典的算法题目,用折半查找对其进行求解的方法十分巧妙,求解代码如下:
//300. Longest Increasing Subsequence
int lengthOfLIS(vector<int>& nums) {
int size=;
vector<int> tail(nums.size()); //候选递增序列集for(auto num:nums){
int i=,j=size;
while(i<j){
int mid=(i+j)/;
if(tail[mid]<num) i=mid+;
else j=mid;
}
tail[i]=num;
if(i==size) size++;
}
return size;
}
以上设定LIS候选序列集 tail,对无序区间 nums 中的各个值通过折半查找的方法,找到其落在 tail 的位置,最终最长的序列长度即为所求。详细算法过程说明见 这里 这里
LeetCode编程训练 - 折半查找(Binary Search)的更多相关文章
- 算法与数据结构基础 - 折半查找(Binary Search)
Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...
- LeetCode编程训练 - 合并查找(Union Find)
Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- STL之二分查找 (Binary search in STL)
STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...
- LeetCode 704. 二分查找(Binary Search)
704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...
- Leetcode 编程训练
Leetcode这个网站上的题都是一些经典的公司用来面试应聘者的面试题,很多人通过刷这些题来应聘一些喜欢面试算法的公司,比如:Google.微软.Facebook.Amazon之类的这些公司,基本上是 ...
- Leetcode 编程训练(转载)
Leetcode这个网站上的题都是一些经典的公司用来面试应聘者的面试题,很多人通过刷这些题来应聘一些喜欢面试算法的公司,比如:Google.微软.Facebook.Amazon之类的这些公司,基本上是 ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- 生活英语读写MOOC-Literature Tutor-有声名著阅读推荐
生活英语读写MOOC-Literature Tutor-有声名著阅读推荐 1. Alice's Adventures in Wonderland 爱丽丝漫游奇境记 音频与文本下载地址:链接:http: ...
- html如何实现圆角的百度搜索框?
<form action="http://www.baidu.com/baidu" target="_blank"> <input type= ...
- thymleaf th:if判断某值不为空
简单描述:判断后台传递过来的值,是否为空,来做一些业务上的处理. 代码: <div class="col-md-6" th:if="${not #strings.i ...
- SQL优化(面试题)
因为现在面试经常需要问的需要SQL优化,问的具体操作步骤时候的常见做法,所以网上总结这些操作步骤: SQL优化的具体操作: 1.在表中建立索引,优先考虑where.group by使用到的字段. 2. ...
- mysql 字符串去掉指定字符
如:在每一列meeting_persons的现有内容之上,去掉15112319字符串 ','')
- BLO(bzoj1123)
Description Byteotia城市有n个 towns, m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n和 ...
- Vuejs自定义select2指令
在做select2插件的时候遇到一些坑,最终解决如下: Vue.directive('select2', { inserted: function (el, binding, vnode) { var ...
- C++一个类对象的大小计算
计算一个类对象的大小时的规律: 1.空类.单一继承的空类.多重继承的空类所占空间大小为:1(字节,下同): 2.一个类中,虚函数本身.成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空 ...
- hiper工具查看页面加载时间
先需要下载 cnpm i hiper -g # 当我们省略协议头时,默认会在url前添加`https://` # 最简单的用法 hiper baidu.com # 如何url中含有任何参数,请使用双引 ...
- html_Dom
Document: 每个载入浏览器的HTML文档都会成为一个Document对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 并且Document 对象是 Wi ...