当前位置:网站首页>Leetcode advanced path - Search insertion location
Leetcode advanced path - Search insertion location
2022-06-10 21:18:00 【Li_ XiaoJin】
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 .
You can assume that there are no duplicate elements in the array .
Example 1:
Input : [1,3,5,6], 5
Output : 2
Example 2:
Input : [1,3,5,6], 2
Output : 1
Example 3:
Input : [1,3,5,6], 7
Output : 4
Example 4:
Input : [1,3,5,6], 0
Output : 0
- Tips in the title “ Given a sort array ”, So we can think of the binary search algorithm . Two points search (Binary Search) It is also called half search . Binary search has two requirements , One is that the sequence of numbers is orderly , The other is that the sequence uses a sequential storage structure ( For example, array ). About several common search algorithms , You can learn it by yourself later , It's quite common .
You can look at ,http://data.biancheng.net/view/122.html
public class SearchInsertPosition {
public int searchInsert(int[] nums, int target) {
int temp = 0;
if (nums.length == 1) {
if (nums[0] >= target) {
return temp;
} else {
temp++;
return temp;
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return i;
} else {
if (target > nums[i]) {
temp++;
} else if (target < nums[i]) {
return i;
}
}
}
return temp;
}
/**
* Two points search
* @param nums
* @param target
* @return
*/
public int searchInsert1(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1, ans = n;
while (left <= right) {
int mid = ((right - left) >> 1) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
public static void main(String[] args) {
SearchInsertPosition search = new SearchInsertPosition();
int[] nums = new int[]{1,3,5,6};
// int[] nums = new int[]{1};
System.out.println(search.searchInsert1(nums, 7));
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/2020-09-09-14-24-27
边栏推荐
- 72. 编辑距离 ●●●
- Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
- Quick start to elastic job, three minutes to experience distributed scheduled tasks
- ROS virtual time
- MySQL service startup failed
- Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
- You have to learn math to play art?
- 电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
- 实用 | 如何利用 Burp Suite 进行密码爆破!
- LeetCode 进阶之路 - 169.多数元素
猜你喜欢

面试必备——synchronized底层原理的基础知识

「运维有小邓」自助帐户解锁工具

Meetup预告:Linkis新版本介绍以及DSS的应用实践

Can you still have a wonderful life if you are laid off at the age of 35?

牛客网:数组中出现次数超过一半的数字

Construction of RT thread smart win10 64 bit compilation environment

LeetCode:497. 非重叠矩形中的随机点————中等

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

游戏兼容性测试(通用方案)

app测试用例
随机推荐
Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
Power set V4 recursion of brute force method /1~n
shell实现ssh登录并执行命令
获取的网络时间 + 时区(+8)
ros虚拟时间
^29事件循环模型
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
LeetCode 进阶之路 - 删除排序数组中的重复项
【电脑使用】如何设置没有自启项的软件开机启动
Attack and defense drill | network security "whistleblower": security monitoring
蛮力法/1~n的全排列 v3 递归
数据库系统概论 ---- 第一章 -- 绪论(重要知识点)
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
pytorch深度学习——卷积操作以及代码示例
synergy: server refused client with our name
一、Vulkan开发理论基础知识
Node (express) implements interfaces such as adding, deleting, modifying, and paging
LeetCode 进阶之路 - 字符串中的第一个唯一字符
你的公司会选择开发数据中台吗?