当前位置:网站首页>4 search insertion location
4 search insertion location
2022-07-27 11:08:00 【DHU yanghualin (UV allergy)】
4 Search insert location
author : Turbo The time limit : 1S chapter : curriculum design
Problem description :
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
Please use a time complexity of O(log n) The algorithm of .
Example 1:
Input :
4
1 3 5 6
5
Output : 2
Example 2:
Input :
4
1 3 5 6
2
Output : 1
Enter description :
Enter three lines :
Enter an integer in the first line n Represents an array nums The length of .
Second line input n An integer represents an array nums The elements of .
In the third line, enter an integer to indicate the target value to be searched target.
Tips :
1 <= n <= 10^4
-10^4 <= nums[i] <= 10^4
nums by No repeating elements Of Ascending Arrange arrays
-10^4 <= target <= 10^4
The output shows that :
Output an integer to represent the result .
Input example :
4
1 3 5 6
7
Output example :
4
#include<iostream>
using namespace std;
void search(int arr[],int target,int length)
{
int left = 0;
int right = length - 1;
if (arr[length - 1] < target)
{
cout << length;
length++;
return;
}
else if (arr[0] > target)
{
cout << 0;
length++;
return;
}
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == target)
{
cout<<mid;
return;
}
else if (arr[mid] < target)
{
left = mid + 1;
}
else if (arr[mid] > target)
{
right = mid - 1;
}
cout<< left;
return;
}
}
int main()
{
int arr[100000];
int length = 0;
cin >> length;
for (int i = 0; i < length; i++)
{
cin >> arr[i];
}
int target = 0;
cin >> target;
search(arr, target, length);
return 0;
}边栏推荐
- IO stream_ Character stream, IO stream summary, IO stream case summary
- Openldap custom schema
- The largest square of leetcode (violence solving and dynamic programming solving)
- MySQL must know and know!!! Reading this article is enough!!!
- Chunjun supports DDL conversion and automatic execution of heterogeneous data sources - dtmo 02 review (including course playback + courseware)
- Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review
- How to create a.Net image with diagnostic tools
- 如何组装一个注册中心
- 【FPGA教程案例40】通信案例10——基于FPGA的简易OFDM系统verilog实现
- pyquery 的使用
猜你喜欢

Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen

YonBuilder赋能创新,用友第四届开发者大赛“金键盘奖”开启竞逐!

IMG SRC is empty or SRC does not exist, and the picture has a white border

BeautifulSoup的使用

How to create a.Net image with diagnostic tools

如何组装一个注册中心

parsel的使用

Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source

计算重叠积分的第二种方法

img src为空或者src不存在,图片出现白色边框
随机推荐
tf.AUTO_ Function of reuse
7z usage
SQL Server2000 database error
迭代次数和熵之间关系的一个验证试验
Edata base, a secondary development project based on spark packaging, is introduced
Li Kou brush question 02 (sum of three numbers + sum of maximum subsequence + nearest common ancestor of binary tree)
[FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
Time and power allocation method to ensure fairness in sensor fusion system
正则form表单判断
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
Local and overall differences between emergence and morphology
Customized modification based on jira7.9.2
The difference of iteration number and information entropy
Synaesthesia integrated de cellular super large-scale MIMO and high-frequency wireless access technology
Problems and Countermeasures of minors' digital security protection
Learning notes - simple server implementation
招聘顶尖人才!旷视科技“MegEagle创视者计划”正式启动
Use of pyquery
Integrated design of communication perception based on CSI: problems, challenges and Prospects
Record of a cross domain problem