【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ]
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it wereinserted in order. You may assume no duplicates in the array.
翻译:给你一个排好序的数组和一个目标值,请找出目标值能够插入数组的位置。
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
public class Solution {
public int searchInsert(int[] A, int target) {
if (A != null) {
for (int i = 0; i < A.length; i++) {
if (target == A[i] || target < A[i]) {
return i;
}
}
return A.length;
}
return -1;
}
public static void main(String[] args) {
int[] arr = { 1, 3, 5, 6 };
System.out.println(new Solution().searchInsert(arr, 5)); // 5 -> 2
System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1
System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4
System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0
}
}
②. 二分查找:时间复杂度log2n
public class Solution {
public int searchInsert(int[] A, int target) {
int mid;
int low = 0;
int high = A.length - 1;
while (low < high) {
mid = (low + high) / 2;
if (A[mid] < target) {
low = mid + 1;
} else if (A[mid] > target) {
high = mid - 1;
} else {
return mid;
}
}
return target > A[low] ? low + 1 : low;
}
public static void main(String[] args) {
int[] arr = { 1, 3, 5, 6 };
System.out.println(new Solution().searchInsert(arr, 5)); // 5 -> 2
System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1
System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4
System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0
}
}
【LeetCode】- Search Insert Position(查找插入的位置)的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode Search Insert Position (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 35 Search Insert Position(找到数的位置Medium)
题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置. 思路:折半查找,和相邻数比较,注意边界 class Solution { public: int searchIns ...
- LeetCode——Search Insert Position
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
- [Leetcode] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Search insert position, 查找插入位置
问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...
随机推荐
- Extjs MVC模式
最近在学习Extjs,发现他可以使用MVC模式,不但可以组织代码,而且可以 减少实现的内容,模型(Models)和控制器(Controllers)也被引入其中. Model模型是字段和它们的数据的集合 ...
- (疯狂java)第二课
(本文章只是为了好玩,没有别的意思,有理解错误之处,恳请提醒,谢谢) 三.数据类型和运算符 想了一下今天看的内容好像依然大脑停留在用C语言去理解java,感觉有点奇怪,为啥本章叫数据类型和运算符,上来 ...
- oracle数据库,mybatis批量insert,缺失values字段
报错:### Error updating database. Cause: java.sql.SQLException: ORA-00926: 缺失 VALUES 关键字### The error ...
- [转] <context-param>与<init-param>的区别与作用
看到一篇关于web.xm文件中标签的讲解,顺带还阐述了容器的工作流程,因此转载此,以供参考,原文地址:与的区别与作用 <context-param>的作用: web.xml的配置中< ...
- <摘录>简述configure、pkg-config、pkg_config_path三者的关系
一.什么是configure 源码安装过程中大多会用到configure这个程序,一般的configure都是一个script,执行时可以传入必要参数告知配置项目. configure程序它会根据传入 ...
- 【mybatis】分别按照 天 月 年 统计查询
页面统计想通过 天 月 年 分别来展示统计效果, 那么查询SQL拼接如下: select *, <if test="groupType == 1"> DATE_FORM ...
- PHP版本切换
前言 php是为了快速构建一个web页面而迅速被大家广为接受的开源语言,通过不断发展已经有了很多的php开源系统,满足了目前大部分用户的站点需求.1995年初php诞生到现在已经存在多个版本,并且每个 ...
- 离线安装ocp3.11需要注意的事情
检查阶段 运行部署前检查的时候 # ansible-playbook -vv playbooks/prerequisites.yml 需要看看play recap是否全过,如果不过需要定位原因,反复执 ...
- Linux内核开发者峰会照的全家福
刚才看到一张Linux内核开发者峰会照的全家福,有历史价值,给大家分享一下.上面有Torvalds(大致在中间).Andrew Morton(目前的内核主要维护者,第二排右数第二个).Alan Cox ...
- http://blog.csdn.net/a9529lty/article/details/6454156
http://blog.csdn.net/a9529lty/article/details/6454156