[ 问题: ]

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 were
inserted in order. You may assume no duplicates in the array.

翻译:给你一个排好序的数组和一个目标值,请找出目标值能够插入数组的位置。


[ 分析: ]
Here are few examples.

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0
注意:一定要考虑一些特殊情况,如数组为null等。


[ 解法: ]
①. 常规解法:从数组索引为0的位置開始找,时间复杂度为O(n),accepted
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(查找插入的位置)的更多相关文章

  1. 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 ...

  2. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  4. Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  6. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. 35 Search Insert Position(找到数的位置Medium)

    题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置. 思路:折半查找,和相邻数比较,注意边界 class Solution { public: int searchIns ...

  8. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  9. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  10. Search insert position, 查找插入位置

    问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...

随机推荐

  1. UML对象图、包图

    对象图(Object Diagram)显示了一组对象和他们之间的关系.使用对象图阿狸说明数据结构,类图中的类或组件等实例的快照.对象图和类图一样,反应了系统的静态过程,但它是以实际的或原型化为基础来表 ...

  2. 【平衡树】【pb_ds】 bzoj1861 [Zjoi2006]Book 书架

    需要用数组记录编号为i的书的位置,和位置i处的书的编号. Code: #include<cstdio> #include<ext/pb_ds/assoc_container.hpp& ...

  3. PS 2019 | Photoshop CC 2019 的安装激活

    文章目录 写在前面 安装步骤 软件激活 关闭"主页"屏幕 写在前面 Photoshop CC 2019(64位)下载地址: 链接:https://pan.baidu.com/s/1 ...

  4. [转]Spring Security 可动态授权RBAC权限模块实践

    RBAC:基于角色的访问控制(Role-Based Access Control) 先在web.xml 中配置一个过滤器(必须在Struts的过滤器之前) <filter> <fil ...

  5. [转]Spring MVC 4常用的那些注解

    Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解.到目前为止,Spring的版 ...

  6. Hiho: 连通图

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 还记得上次小Hi和小Ho学校被黑客攻击的事情么,那一次攻击最后造成了学校网络数据的丢失.为了避免再次出现这样的情况,学校决 ...

  7. NHibernate使用MemCache二级缓存

    首先,当然是安装MemCache服务器端了. 然后配置过程,仅仅两个问题. 1.NHibernate要与NHibernate.Cache的版本要一致.否则,NHibernate.Caches.MemC ...

  8. XMPP资源绑定(Resource Binding)与单台设备登录控制

    原文:http://blog.csdn.net/brasbug/article/details/26353511 一个XMPP的账号由三部分组成: 用户名(user/node),域名(domain)和 ...

  9. iOS中nil、Nil、NULL、NSNull详解

    nil nil 是 ObjC 对象的字面空值,对应 id 类型的对象,或者使用 @interface 声明的 ObjC 对象. 例如: NSString *someString = nil; NSUR ...

  10. Linq之旅:Linq入门详解(Linq to Objects)【转】

    http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html Linq之旅:Linq入门详解(Linq to Objects) 示例代码下载:Linq之 ...