[ 问题: ]

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. while an existing transition or presentation is occurring; the navigation stack will not be updated

    使用UIAlertController提示信息,在之后使用navigation进行逻辑跳转时,出现popToViewController:transition: called on <UINav ...

  2. 网站服务管理系统wdcp系列教程之九

    在我们安装了网站服务管理系统wdcp之后,在使用过程中可能会出现这样或那样的疑问,下面给大家整理几点出来,方便大家学习.还有不懂得话,可以去wdlinux论坛找找相关教程. 1.wdcp如何拒绝或阻止 ...

  3. iOS-Runtime、对象模型、消息转发

    Objective-C只是在C语言层面上加了些关键字和语法.真正让Objective-C如此强大的是它的运行时.它很小但却很强大.它的核心是消息分发. Message 执行一个方法,有些语言.编译器会 ...

  4. jQuery Ajax 参数解析

    简单的例子: $.ajax({ type:"post", data:{a:acon,b:bcon} , url:"ajax.php", async:false ...

  5. Android内存优化2 了解java内存分配 2

    JVM内存模型 Java虚拟机(Java Virtual Machine=JVM)的内存空间分为五个部分,分别是: 1. 程序计数器 2. Java虚拟机栈 3. 本地方法栈 4. 堆 5. 方法区. ...

  6. 配置Node Manager启动同一机器的两个域

    进入/u01/FMW_Home/weblogic/wlserver_10.3/server/bin目录,在需要配置的节点上启动node manager,以weblogic用户登录 c d /u01/F ...

  7. [转]Using the Microsoft Connector for Oracle by Attunity with SQL Server 2008 Integration Services

    本文转自:http://technet.microsoft.com/en-us/library/ee470675(v=sql.100).aspx SQL Server Technical Articl ...

  8. WSDL-学习总结

    1.什么是WSDL 是一种使用 XML 编写的文档.这种文档可描述某个 Web service.它可规定服务的位置,以及此服务提供的操作(或方法). 2.WSDL文档结构: <binding&g ...

  9. 过滤xss攻击和sql注入函数

    /**+----------------------------------------------------------* The goal of this function is to be a ...

  10. 用java源代码学数据结构<七>: BST

    /* * 以int类为例 * 其它的类必须能够比较 * */ //二叉搜索树的节点点 class BSTNode{ int item; BSTNode lc; BSTNode rc; BSTNode ...