[ 问题: ]

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
  1. public class Solution {
  2. public int searchInsert(int[] A, int target) {
  3. if (A != null) {
  4. for (int i = 0; i < A.length; i++) {
  5. if (target == A[i] || target < A[i]) {
  6. return i;
  7. }
  8. }
  9. return A.length;
  10. }
  11. return -1;
  12. }
  13.  
  14. public static void main(String[] args) {
  15. int[] arr = { 1, 3, 5, 6 };
  16. System.out.println(new Solution().searchInsert(arr, 5)); // 5 -> 2
  17. System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1
  18. System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4
  19. System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0
  20. }
  21. }

②. 二分查找:时间复杂度log2n

前提条件:一定是有序数组。

  1. public class Solution {
  2. public int searchInsert(int[] A, int target) {
  3. int mid;
  4. int low = 0;
  5. int high = A.length - 1;
  6. while (low < high) {
  7. mid = (low + high) / 2;
  8. if (A[mid] < target) {
  9. low = mid + 1;
  10. } else if (A[mid] > target) {
  11. high = mid - 1;
  12. } else {
  13. return mid;
  14. }
  15. }
  16.  
  17. return target > A[low] ? low + 1 : low;
  18. }
  19.  
  20. public static void main(String[] args) {
  21. int[] arr = { 1, 3, 5, 6 };
  22. System.out.println(new Solution().searchInsert(arr, 5)); // 5 -> 2
  23. System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1
  24. System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4
  25. System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0
  26. }
  27. }

【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. BZOJ 3676 [Apio2014]回文串(回文树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3676 [题目大意] 考虑一个只包含小写拉丁字母的字符串s. 我们定义s的一个子串t的& ...

  2. mysql表相关操作

    表的完整性约束 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性 主要分为: not null  标识该字段不能为空 default   为该字段设置默认值 unsign ...

  3. getDimension,getDimensionPixelOffset和getDimensionPixelSize

    dimens.xml里写上三个变量: <dimen name="activity_vertical_margin1">16dp</dimen> <di ...

  4. NOIP2014 解题报告·水渣记

    Day 1: 第一次参加noip.小激动,小紧张,这些正常的情绪就不用说了.唯一值得一提的是 我早上步行去郑大工学院的时候迷路了,直接转进了隔壁的河南农大,绕了半天找不到机房,还给几个同学打了电话可就 ...

  5. maven项目修改项目名

    修改pom文件下面三处

  6. 最好的拖拽js

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 用 Jenkins + .netcore 2.0 构建

    上一篇是关于 .net framework 框架的, 今天是关于 .netcore 2.0 的 这里为大家分享 配置中踩到的坑 cd %WORKSPACE%\"需要还原的项目目录" ...

  8. RES协议

    MFC 通过HTML访问内部资源 资源导入JPG的图片,资源对应ID是137 <img src="res:/JPG/#137" width="100%" ...

  9. asp.net membership 修改密码

    aspnet_Applications 存储数据库所涉及应用程序有关信息. aspnet_Membership 存储与用户相关信息例如:用户登陆密码,创建时间. aspnet_Paths 存储应用程序 ...

  10. zigbee 学习笔记

    在德州仪器的站点:http://www.ti.com.cn/tool/cn/z-stack上下载安装zigbee2007协议栈版,我的是ZStack-CC2530-2.3.0-1.4.0. 以下演示一 ...