当前位置:网站首页>LeetCode 进阶之路 - 搜索插入位置
LeetCode 进阶之路 - 搜索插入位置
2022-06-10 20:01:00 【Li_XiaoJin】
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
示例 1:
输入: [1,3,5,6], 5
输出: 2
示例 2:
输入: [1,3,5,6], 2
输出: 1
示例 3:
输入: [1,3,5,6], 7
输出: 4
示例 4:
输入: [1,3,5,6], 0
输出: 0
- 题目中提示 “给定一个排序数组”,由此可以想到二分查找算法。二分查找(Binary Search)也叫作折半查找。二分查找有两个要求,一个是数列有序,另一个是数列使用顺序存储结构(比如数组)。关于几种常见的查找算法,后面可以单独学习一下,挺常见的。
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;
}
/**
* 二分查找
* @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: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/2020-09-09-14-24-27
边栏推荐
- 中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
- NetWorkX使用方法及 nx.draw()相关参数
- 记录一下今天的MySQL故障
- Cas de test app
- 详解三级缓存解决循环依赖
- Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
- 72. 编辑距离 ●●●
- You have to learn math to play art?
- Theoretical basis of distributed services
- mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
猜你喜欢

synergy: server refused client with our name

堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡

Knowledge map / relationship visualization

AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘

canvas 高级功能(上)

Attack and defense drill | network security "whistleblower": security monitoring

pdf.js-----js解析pdf文件實現預覽,並獲取pdf文件中的內容(數組形式)

Redis缓存雪崩

synergy: server refused client with our name

Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
随机推荐
Redis缓存击穿
Redis set password command (temporary password)
保姆级教程:如何成为Apache Linkis文档贡献者
Node (express) implements interfaces such as adding, deleting, modifying, and paging
Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
Identity and access management (IAM)
app测试用例
Read the source code of micropyton - add the C extension class module (2)
分布式服务理论基础
Explain L3 cache to solve circular dependency
^30H5 Web Worker多线程
35岁被裁员,还能拥有美妙人生吗?
Steps to build a personal website using servers purchased by Alibaba cloud international
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
app測試用例
Can you still have a wonderful life if you are laid off at the age of 35?
【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
Pytorch deep learning -- convolution operation and code examples
What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list