[ 问题: ]

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 -> 2System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4System.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 -> 2System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0}
}

【LeetCode】- Search Insert Position(查找插入的位置)相关推荐

  1. leetcode -- Search Insert Position

    2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...

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

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

  3. LeetCode --Search Insert Position

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

  4. Leetcode: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 搜索插入位置

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

  6. [LeetCode]Search Insert Position

    原题链接:http://oj.leetcode.com/problems/search-insert-position/ 题意描述: Given a sorted array and a target ...

  7. LeetCode - 35. Search Insert Position

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  8. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  9. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  10. leetcode python3 简单题35. Search Insert Position

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十五题 (1)题目 英文: Given a sorted array and a ...

最新文章

  1. 关于Iframe在IE6下不显示的bug
  2. 最新!2021 中国内地大学 ESI 排名出炉:326 所高校上榜!
  3. 实现lua面向对象的private属性
  4. Java命令学习系列
  5. How to resolve ATC error message Package Violation (Error) - Missing Use Access (USEM)
  6. C++模版和C#泛型求同存异录(一)sizeof(T)
  7. [js] 实现一个函数记忆的方法
  8. python整数转换字符串_Python | 将字符串转换为整数列表
  9. linux c 串口可读可写,串口编程可写入不能读取 怎么解决
  10. ichartjs android,在android上动态实现ichartjs的3D柱形图
  11. 一个人的生活可以简约到什么程度?
  12. C#—接口和抽象类的区别?
  13. 必读的android 文章- 收藏集 - 掘金
  14. NorthWind 数据库整体关系
  15. 无线城市--WiMax,WiFi-Mesh和3G/4G/5g网络
  16. 腾讯阿里的螺丝钉,一样会生锈!
  17. 阿尔法python 第四章 程序的控制结构
  18. PS 色调——颜色运算
  19. 请简要说明西门子PLC1500的主要功能
  20. 2019河南对口升学高考试卷计算机专业课,2019年河南省对口升学:考试和录取

热门文章

  1. iphonex如何关机_iphonex常用手势操作有哪些 iphonex常用手势操作介绍【详解】
  2. python编写的程序大全_Python开发技术大全
  3. python决策树生成规则_如何从scikit-learn决策树中提取决策规则?
  4. 【技术综述】深度学习在自然语言处理中的应用发展史
  5. 【AI初识境】被Hinton,DeepMind和斯坦福嫌弃的池化,到底是什么?
  6. WinForm界面开发之布局控件WeifenLuo.WinFormsUI.Docking的使用
  7. 好品山东谋定产业扶贫-农业大健康·万祥军:乡村振兴行动
  8. 【转载】一个男人关心的东西 决定了他的层次
  9. es6中export和export default的区别
  10. WPF整理-使用逻辑资源