当前位置:网站首页>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.
边栏推荐
- MySQL 5.7 and sqlyogv12 installation and use cracking and common commands
- 大学生参加六星教育PHP培训,找到了薪水远超同龄人的工作
- 做题笔记2(两数相加)
- Microsoft question 100 - do it every day - question 11
- NoSQL introduction practice notes I
- Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
- Leetcode learn complex questions with random pointer linked lists (detailed explanation)
- Some opinions on bug handling
- Im im development optimization improves connection success rate, speed, etc
- Text filtering skills
猜你喜欢

Sort 5-count sort

Sort 2 bubble sort and quick sort (recursive and non recursive explanation)

结构化设计的概要与原理--模块化

【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)

Call DLL file without source code

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

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)

Leetcode learn to insert and sort unordered linked lists (detailed explanation)

Interesting kotlin 0x07:composition

WSL+Valgrind+Clion
随机推荐
Hdu1847 problem solving ideas
Alibaba cloud - Wulin headlines - site building expert competition
Debugging methods of USB products (fx3, ccg3pa)
Interesting kotlin 0x09:extensions are resolved statically
Applet: get element node information
[learn slam from scratch] publish the coordinate system transformation relationship to topic TF
Record development issues
我该如何理解工艺
MySQL安装教程
Best Cow Fences 题解
Outline and principle of structured design -- modularization
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
HyperMesh auto save (enhanced) plug-in instructions
MD5加密验证
Implementation of paging
做题笔记4(第一个错误的版本,搜索插入位置)
Some opinions on bug handling
【深度学习】:《PyTorch入门到项目实战》第九天:Dropout实现(含源码)
Quickly master kotlin set functions
时间复杂度