当前位置:网站首页>做题笔记4(第一个错误的版本,搜索插入位置)
做题笔记4(第一个错误的版本,搜索插入位置)
2022-07-28 16:02:00 【竹林观月】
题目
一、题目一
1.代码
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int min=1;
int max=n;
while(min<=max)
{
int mid=min+(max-min)/2;
if(isBadVersion(mid))
{
max=mid;
}
else if(isBadVersion(mid)==0)
{
min=mid+1;
}
if(min==max)
{
return min;
}
}
return min;
}
};
2.其他
1)找出第一个错误的条件是最小值与最大值相等。
2)中间值为错误,中间值有可能为第一个错误的,故将其赋值给新的最大值;但若中间值不为错误的,则新的最小值为该中间值加1。
二、题目二
1.代码
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int min = 0;
int max = n - 1;
while (min <= max)
{
int mid = min + (max - min) / 2;
if (nums[mid] < target)
{
min = mid + 1;
}
else if (nums[mid] >= target)
{
max = mid - 1;
}
}
return min;
}
};
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int min = 0;
int max = n-1;
int an = 0;
int b = 0;
while (min <= max)
{
int mid = min + (max - min) / 2;
if (nums[mid] < target)
{
min = mid + 1;
}
else if (nums[mid] > target)
{
max = mid - 1;
}
else if (nums[mid] == target)
{
an = mid;
b = 1;
break;
}
}
if(b==0)
{
an=min;
}
return an;
}
};
2.其他
上述两种方法的区别在于对非数组中的数的定位,第一种解决方案中,min的左边一定小于target,max的右边一定大于target。
边栏推荐
- CRC16 data verification supports modelbus and XMODEM verification modes (C language)
- Cluster construction and use of redis5
- “蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
- 有趣的 Kotlin 0x06:List minus list
- Splash (rendering JS service) introduction installation
- Multiple commands produce ‘.../xxx.app/Assets.car‘问题
- Oracle table partition
- Mysql与Oracle的13点区别
- MySQL 5.7 and sqlyogv12 installation and use cracking and common commands
- Learn ABAQUS script programming script in an hour
猜你喜欢

Sort 1-insert sort and Hill sort

HM secondary development - data names and its use

【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)

排序2-冒泡排序与快速排序(递归加非递归讲解)

Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)

Interesting kotlin 0x08:what am I

【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)

ANSYS secondary development - MFC interface calls ADPL file

WSL+Valgrind+Clion

nowcode-学会删除链表中重复元素两题(详解)
随机推荐
Redis source code optimization -- binding core
nowcode-学会删除链表中重复元素两题(详解)
Interesting kotlin 0x08:what am I
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
【深度学习】:《PyTorch入门到项目实战》第八天:权重衰退(含源码)
Ansa secondary development - apps and ansa plug-in management
【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导
【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)
LwIP develops | socket | TCP | keepalive heartbeat mechanism
微软100题-天天做-第16题
Fx3 development board and schematic diagram
【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
队列的介绍与实现(详解)
Li Hongyi, machine learning 5. Tips for neural network design
USB产品(FX3、CCG3PA)的调试方法
有趣的 Kotlin 0x07:Composition
Interesting kotlin 0x09:extensions are resolved statically
Debugging methods of USB products (fx3, ccg3pa)
微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能