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

分析:二分查找的变体。代码如下:

 1 class Solution {
 2 public:
 3     int searchInsert(int A[], int n, int target) {
 4         int l = 0, r = n - 1;
 5
 6         while(l <= r){
 7             int mid = (l + r)/2;
 8             if(A[mid] == target) return mid;
 9             if(A[mid] < target) l = mid + 1;
10             if(A[mid] > target) r = mid - 1;
11         }
12
13         return l;
14     }
15 };

转载于:https://www.cnblogs.com/Kai-Xing/p/4206421.html

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 搜索插入位置

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

  3. [LeetCode]Search Insert Position

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

  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 (二分查找)

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

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

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

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

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

  8. LeetCode - 35. Search Insert Position

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

  9. leetcode 【 Search Insert Position 】python 实现

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

最新文章

  1. Swift学习——Swift解释特定的基础(七)
  2. 「翻译」SAP零售预测和补货–简要概述
  3. ORACLE ORA-01653: unable to extend table 的错误
  4. vue入门教程(二)
  5. 二叉树的前中后序遍历之迭代法(非统一风格迭代方式)
  6. SpringCloud与Seata分布式事务初体验
  7. rm: 无法删除swap: 不允许的操作_safe-rm老板再也不用担心我删库跑路啦[视频]
  8. 显示器说:偶好惨啊,每天给人看。
  9. 一组开源asp.net用户控件
  10. Smart View的客户化开发
  11. 移动端Vue3框架demo
  12. 福禄克CFP2-100-Q与OFP2-100-Q区别
  13. android发送语音动画,Android仿微信发送语音消息的功能及示例代码
  14. 一步一步实现STM32-FOTA系列教程之STM32-FLASH分区说明
  15. 【codeforces 707C】Pythagorean Triples
  16. 浅谈java中的ServerSocket和Socket的通信原理实现聊天及多人聊天
  17. 基于tiny4412的u-boot移植(二)_ git clone
  18. 3D建模软件应该学中文版还是英文版?Maya软件应该学中文版还是英文版?
  19. 解析迅捷产品的八大特点
  20. C语言和win32绿色鼠标连点器,带代码(Windows)

热门文章

  1. MySQL alter
  2. mysql5.6安装及实现双向备份
  3. NHibernate使用时,不能返回自己的异常的解决办法
  4. linux命令dd创建虚拟硬盘,每日一题.PYTHON如何模拟LINUX的dd命令快速创建大文件?...
  5. 反弹模型(bounce model)----adot, H, Hdot变化图
  6. 从头开始学习深度学习之卷积
  7. 云容器实例服务入门必读
  8. PHP MySQL Update
  9. 试用MarkDown
  10. Python_Day4_函数