问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置。

算法分析:依旧利用二分查找算法。

 public int searchInsert(int[] nums, int target)
{
return binarySearch(nums, 0, nums.length - 1, target);
}
public int binarySearch(int[] nums, int left, int right, int target)
{
int mid = (left + right)/2;
if(left > right)
{
return left;
}
if(nums[mid] == target)
{
return mid;
}
else if(nums[mid] < target)
{
return binarySearch(nums, mid + 1, right, target);
}
else
{
return binarySearch(nums, left, mid - 1, target);
}
}

Search insert position, 查找插入位置的更多相关文章

  1. [LeetCode] Search Insert Position 搜索插入位置

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

  2. [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 ...

  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] 35. Search Insert Position 搜索插入位置

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

  5. lintcode:Search Insert Position 搜索插入位置

    题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...

  6. 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】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  8. 035 Search Insert Position 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...

  9. 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 ...

随机推荐

  1. office 2010 自动连接网络打印机的问题(保存或者打开极慢) 解决方法

    将默认打印机设为本地打印机或 Microsoft XPS Document Writer

  2. 10.php引用(&)详解及注意事项

    <?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条 ...

  3. [luogu4556]雨天的尾巴

    [luogu4556]雨天的尾巴 luogu 发现是一顿子修改然后再询问,那么把修改树上差分一下再线段树合并 但是... 如果你只有35分... https://www.luogu.org/discu ...

  4. Mark一下 mysql 误删除root用户的解决方法

    今天学习mysql用户管理,不小心将mysql.user表中的root用户给删掉了,然后就无法登录mysql了,网上找到了linux下的解决方法,我做了简单的修改,改成了我的windows版,恢复方法 ...

  5. MySQL 数据备份,Pymysql模块(Day47)

    阅读目录 一.IDE工具介绍 二.MySQL数据备份 三.Pymysql模块 一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...

  6. for迭代序列的三种方式

    while循环是条件性的,for循环是迭代性的. for循环会访问所有迭代对象中的所有元素,并在所有条目都结束后结束循环. for循环迭代序列有三种基本的方式,分别是通过序列项迭代.通过索引迭代.通过 ...

  7. 基于WinIO 3.0实现驱动级键盘模拟输入

    基于WinIO 3.0实现驱动级键盘模拟输入 一个业务场景需要使用驱动级的键盘模拟,折腾了2天,总结一下,为后人节省时间. 限制条件: 1.需要真实PC机,虚拟机不行 2.仅支持PS/2 键盘(指外接 ...

  8. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  9. 14链表中倒数第k个结点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   思路: 快慢指针 快指针 先走k 步, 然后快慢指针一起走 当快指针走到null 时, 慢指针就是所求的倒数第k个节点 tips: 判断k是否 ...

  10. PAT 天梯赛 L1-005. 考试座位号 【MAP标记】

    题目链接 https://www.patest.cn/contests/gplt/L1-005 题意 有一个 考生号,一个试机座位,一个考试座位,给出试机座位,查询 考生号和考试座位 思路 MAP + ...