[ 问题: ]

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. [bzoj3244][noi2013]树的计数 题解

    UPD: 那位神牛的题解更新了,在这里. ------------------------------------------------------------------------------- ...

  2. JDBC 编程初步

    JDBC 概述 什么是JDBC 是一种用于执行SQL语句的Java API,它由一组用Java语言编写的类和接口组成,JDBC提供了一种操作数据的标准,JDBC的目标是使Java程序员使用JDBC可以 ...

  3. [BZOJ1860][ZJOI2006]Mahjong(DP)

    1860: [Zjoi2006]Mahjong麻将 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 412  Solved: 248[Submit][Sta ...

  4. 【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here

    给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/j ...

  5. 【深度优先搜索】mr353-取奶

    应该是USACO的题目,暂时没有找到对应出处. [题目大意] 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装 ...

  6. Maven的内置属性

    Maven共有6类属性: ①内置属性(Maven预定义属性,用户可以直接使用) ${basedir}表示项目的根路径,即包含pom.xml文件的目录 ${version}表示项目版本 ${projec ...

  7. 详解React的生命周期

    React生命周期 之前自己在学习React的时候,只是简单的理解了生命周期有这么一些,但是不知道大概的一个流程是怎么样的.那天在面试的时候,问到了.自己也有点懵,也没提前看,不过还是答上来了一些,这 ...

  8. iOS 日志管理异常捕获组件LFLogManager

    一.功能 1.分级打印保存 解决一大堆重要.不重要打印信息在控制台混为一团的尴尬局面.可设置仅打印某级别及以上的信息.分为5类打印: DDLogError(@"打印并保存特别重要信息&quo ...

  9. HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. SQL Server--CheckPoint

    http://www.cnblogs.com/TeyGao/category/526201.html