当前位置:网站首页>Question note 4 (the first wrong version, search the insertion position)
Question note 4 (the first wrong version, search the insertion position)
2022-07-28 16:57:00 【Watching the moon in the bamboo forest】
List of articles
subject
First wrong version
Search insert location
One 、 Topic 1
1. Code
// 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. other
1) The condition for finding the first error is that the minimum value is equal to the maximum value .
2) Intermediate value is wrong , The intermediate value may be the first wrong , So assign it to the new maximum ; But if the intermediate value is not wrong , Then the new minimum value is the intermediate value plus 1.
Two 、 Topic two
1. Code
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. other
The difference between the above two methods lies in the positioning of numbers in non array , In the first solution ,min The left side of the must be smaller than target,max The right side of must be larger than target.
边栏推荐
- Simple addition, deletion, modification and query of commodity information
- Oracle system composition
- The local area network cannot access the Apache server
- MySQL5.7及SQLyogV12安装及使用破解及常用命令
- Signal shielding and processing
- Signal and slot mechanism of QT learning
- 【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)
- Efficiency comparison of three methods for obtaining timestamp
- asmlinkage的理解
- Microsoft question 100 - do it every day - question 16
猜你喜欢

Interesting kotlin 0x0a:fun with composition

Ansa secondary development - apps and ansa plug-in management

大学生参加六星教育PHP培训,找到了薪水远超同龄人的工作

快速掌握 Kotlin 集合函数

【深度学习】:《PyTorch入门到项目实战》第九天:Dropout实现(含源码)

MySQL5.7及SQLyogV12安装及使用破解及常用命令

【深度学习】:《PyTorch入门到项目实战》第八天:权重衰退(含源码)

小程序:获取元素节点信息

有趣的 Kotlin 0x07:Composition

LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数
随机推荐
【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)
Quickly master kotlin set functions
LeetCode每日一练 —— 160. 相交链表
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
我该如何理解工艺
做题笔记2(两数相加)
Design direction of daily development plan
Some opinions on bug handling
Leetcode70 suppose you are climbing stairs. You need n steps to reach the roof. You can climb one or two steps at a time. How many different ways can you climb to the roof?
Im im development optimization improves connection success rate, speed, etc
Leetcode daily practice - 160. Cross linked list
Some suggestions on Oracle SQL tuning
"Weilai Cup" 2022 Niuke summer multi school training camp 3 h.hacker sam+ segment tree /dp/ divide and conquer (without the largest sub segment and of the inspection interval)
Implementation of paging
【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)
Redis series 4: sentinel (sentinel mode) with high availability
Reset grafana login password to default password
在AD中添加差分对及连线
【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
parseJson