当前位置:网站首页>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
边栏推荐
- LeetCode:497. Random points in non overlapping rectangles -- medium
- 蛮力法/1~n的全排列 v3 递归
- Read the source code of micropyton - add the C extension class module (3)
- [generation confrontation network learning part I] classic Gan and its existing problems and related improvements
- Redis缓存穿透
- 1、 Vulkan develops theoretical fundamentals
- 游戏兼容性测试(通用方案)
- Li Kou 10821084 solution_ Question of SQL query type
- Solve the problem that the idea automatically becomes * when there are more than 5 identical packages
- Mixin -- mixed
猜你喜欢

Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"

Explain L3 cache to solve circular dependency

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日

Software definition boundary (SDP)

A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb

服务管理与通信,基础原理分析

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

^29 event cycle model

CET-6 - Business English - the last recitation before the test

Theoretical basis of distributed services
随机推荐
记录一下今天的MySQL故障
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list
pdf.js-----js解析pdf文件實現預覽,並獲取pdf文件中的內容(數組形式)
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
Steps to build a personal website using servers purchased by Alibaba cloud international
Portable FDW framework for Pb
解决idea超过5个相同包的时候自动变成*的问题
自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
2台电脑共享一套键盘鼠标
中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
LeetCode:1037. Effective boomerang - simple
Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
冷酸灵,一个国产品牌IPO的30年曲折史
Construction of RT thread smart win10 64 bit compilation environment
Game compatibility test (general scheme)
torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
蛮力法/邻接表 深度优先 有向带权图 无向带权图