1、问题

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

2、分析

我们这里用二分法,然后要注意的是,获取中间的下标是开始下标加末尾小标除以2,而不是末尾下标减去开始下标除以2,这里以后要注意。

3、代码实现

public static int searchInsert(int[] nums, int target) {int index = 0;if (nums == null || nums.length == 0) {return index;}if (target <= nums[0]) {return 0;}int length = nums.length;int start = 0;int end = length;while (start <= end) {int mid = (end + start) / 2;if (target < nums[mid]) {end = mid;} else if (target > nums[mid]){start = mid;} else  {return mid;}if (start - end == -1) {return start + 1;}}return index;}

LeetCode之Search Insert Position相关推荐

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

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

  2. LeetCode - 35. Search Insert Position

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

  3. leetcode 【 Search Insert Position 】python 实现

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

  4. LeetCode T35 Search Insert Position

    文章目录 题目地址 题目描述 思路 题解 题目地址 中文:https://leetcode-cn.com/problems/search-insert-position/ 英文:https://lee ...

  5. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  6. leetcode 35 Search Insert Position(二分法)

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

  7. [LeetCode] 35. Search Insert Position

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

  8. LeetCode 35. Search Insert Position

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

  9. [LeetCode]--35. Search Insert Position

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

最新文章

  1. 在Cacti上实现MSN报警机制
  2. python常见的语法错误_python常见语法错误有什么
  3. SoringMVC-常用注解标签详解(摘抄)
  4. python画网络关系 节点和边存在文件里_python复杂网络分析库NetworkX
  5. Struts如何获取客户端ip地址
  6. 云表中表单配置内嵌浏览器
  7. 和计算机与设计相关的,计算机设计和类论文参考文献 计算机设计和参考文献有哪些...
  8. Three.js入门详解
  9. 怎么用虚拟机安装Windows XP?
  10. qt.qpa.xcb: could not connect to display解决
  11. 《推荐系统》基于标签的用户推荐系统
  12. 5个免费、优质视频素材网站,可商用
  13. android连接苹果蓝牙耳机,苹果蓝牙耳机怎么配对 苹果蓝牙耳机怎么配对安卓手机 苹果蓝牙耳机使用说明...
  14. 银行数字化转型导师坚鹏:数字化背景下BLM银行网点转型
  15. Columns函数:返回数据表区域的总列数。
  16. 豆瓣首页话题输入框的实现
  17. windows下cmd命令(全面)更新版
  18. 微信公众号里放XLS链接教程
  19. 软件开发获取客户需求的十大沟通技巧
  20. 昇腾 (Ascend) AI 处理器:达芬奇架构

热门文章

  1. Windows 11 预览版 Build 22000.120 发布
  2. [007] 详解 .NET 程序集
  3. C# 外接(网口)双摄像头视频获取
  4. 使用 gRPCurl 调试.NET 5的gPRC服务
  5. 听说用 C# 写 TensorFlow 更高效?
  6. 把Autofac玩的和java Spring一样6
  7. 使用 Docker 搭建 PostgreSQL 12 主从环境
  8. Istio1.5 Envoy 数据面 WASM 实践
  9. IT从业者的迷思与求解之道——座谈会实录摘选
  10. 使用Kubeadm创建k8s集群之节点部署(三十二)