当前位置:网站首页>Question making note 3 (two point search)
Question making note 3 (two point search)
2022-07-28 16:57:00 【Watching the moon in the bamboo forest】
List of articles
subject
One 、 In order to find
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
int a = -1;
for(int i=0;i<n;i++)
{
if (nums[i] == target)
{
a = i;
}
}
return a;
}
};
Two 、 Two points search
1. Code
class Solution {
public:
int search(vector<int>& nums, int target) {
int min = 0;
int max = nums.size() - 1;
int mid = 0;
while (min <= max)
{
mid = min + (max - min) / 2;
if (nums[mid] > target)
{
max = mid - 1;
}
else if (nums[mid] < target)
{
min = mid + 1;
}
else if (nums[mid] == target)
{
return mid;
}
}
return -1;
}
};
2. Array subscript out of bounds and memory overflow
1) In the above topic , use max+min Risk of memory overflow , When two int Type variable addition , The result may be greater than int The maximum range that type can represent .
2) The subscript of the array is out of bounds , Because the memory that does not belong to the array is accessed , For example, an array with three elements is applied a[3], If you visit a[3] It will cause the subscript to cross the boundary , because a[3] Does not belong to this array .
边栏推荐
- Asp.net large file block upload breakpoint resume demo
- 日常开发方案设计指北
- 【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)
- Some suggestions on optimizing HyperMesh script performance
- Re10:读论文 Are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr
- Brother Ali teaches you how to correctly understand the problem of standard IO buffer
- 【深度学习】:《PyTorch入门到项目实战》第九天:Dropout实现(含源码)
- leetcode70假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
- Redis source code optimization -- binding core
- Interesting kotlin 0x07:composition
猜你喜欢

小程序:scroll-view默认滑倒最下面

Interesting kotlin 0x09:extensions are resolved statically

微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能

Several methods of HyperMesh running script files

Do you really understand CMS garbage collector?

College students participated in six Star Education PHP training and found jobs with salaries far higher than those of their peers

Alibaba cloud MSE supports go language traffic protection

在AD中添加差分对及连线

Brother Ali teaches you how to correctly understand the problem of standard IO buffer

Re12:读论文 Se3 Semantic Self-segmentation for Abstractive Summarization of Long Legal Documents in Low
随机推荐
Implementation of paging
Signal and slot mechanism of QT learning
Learn ABAQUS script programming script in an hour
Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints
Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
Oracle system composition
做题笔记4(第一个错误的版本,搜索插入位置)
综合设计一个OPPE主页--页面服务部分
Given positive integers n and m, both between 1 and 10 ^ 9, n < = m, find out how many numbers have even digits between them (including N and m)
海康威视回应'美国禁令'影响:目前所使用的元器件都有备选
【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)
累计出货130亿颗Flash,4亿颗MCU!深度解析兆易创新的三大产品线
Microsoft question 100 - do it every day - question 11
HyperMesh auto save (enhanced) plug-in instructions
Some suggestions on Oracle SQL tuning
【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)
Installation of QT learning
Outline and principle of structured design -- modularization
Splash (rendering JS service) introduction installation
HTAP是有代价的