问题描述:给定一个有序序列,如果找到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. Oracle的聚合函数group by结合CUBE和ROLLUP的使用

    转自:https://docs.oracle.com/cd/E11882_01/server.112/e25554/aggreg.htm#DWHSG8618 CUBE Syntax CUBE appe ...

  2. 【微信】QQ邮箱助手不提醒解决

    1.问题及原因: 微信上的QQ邮箱提醒功能失效了,收到信后不提醒.忘了是使用了TIM,还是使用了QQ邮箱之后才出现的. 不过原因是:QQ邮箱提醒被设置为仅在QQ邮箱客户端提醒 2.解决: 重新安装QQ ...

  3. python抓取网页中的动态数据

    一.概念 网页中的许多数据并不是写死在HTML中的,而是通过js动态载入的.所以也就引出了什么是动态数据的概念,动态数据在这里指的是网页中由Javascript动态生成的页面内容,是在页面加载到浏览器 ...

  4. String.prototype.charCodeAt()

    w <script> function wf(w) { console.log(w) } var w = 'ABC'.charCodeAt(0); wf(w) var w = '中'.ch ...

  5. IO流入门-第三章-FileInputStream_FileOutputStream复制

    利用FileInputStream和FileOutputStreamj进行复制粘贴 /* 文件复制粘贴 */ import java.io.*; public class FileInput_Outp ...

  6. python的进程与线程

    一.进程与线程的相关概念 1.什么是进程 进程是一个程序在一个数据集上的一次动态执行过程. 进程一般由程序,数据集,进程控制块三部分组成. 2.什么是线程 线程也叫轻量级进程,它是一个基本的CPU执行 ...

  7. java 内存空间

    堆:new 出的对象在堆上 java栈:java程序.线程运行数据.内存数据 每个方法都有自己的栈.运行时需要的数据存在自己的栈中 每个线程对立的是图中浅蓝色的部分(java栈.本地方法栈.程序计数器 ...

  8. Python 模块之Logging——常用handlers的使用

    一.StreamHandler 流handler——包含在logging模块中的三个handler之一. 能够将日志信息输出到sys.stdout, sys.stderr 或者类文件对象(更确切点,就 ...

  9. 简明python教程二-----对象

    物理行:是你在编写程序时所看见的. 逻辑行:是Python看见的单个语句. 默认的,python希望每行都只使用一个语句,这样使得代码更加易读. 如果你想要在一个物理行中使用多于一个逻辑行,用分号“: ...

  10. HDU1838:Chessboard(线性dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1838 这题也挺不错的.首先题目说了,棋盘的右下角一定是'1',另外棋盘里面至少包含一个1,所以最小值是1, ...