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.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 1:

Input: [1,3,5,6], 0
Output: 0 很简单的一个题目,找有序数列里的目标数的下标,找不到就返回可以插入的下标。既然已经排好序,直接用二分法查找即可,一次提交就A掉。上代码。
public static int searchInsert(int[] nums, int target) {
if (nums.length == 0||target<nums[0])
return 0;
if(target>nums[nums.length-1])return nums.length; int low, high;
low = 0;
high = nums.length - 1;
while (nums[(low + high) / 2] != target) {
if (low<high) {
if (nums[(low + high) / 2] > target) { high = (low + high) / 2;
} else if (nums[(low + high) / 2] < target) {
if(low==(low + high) / 2)return (low + high) / 2+1;
low = (low + high) / 2;
} else {
return (low + high) / 2;
}
}
} return (low + high) / 2;
}

Leetcode 35——Search Insert Position的更多相关文章

  1. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  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 35] Search Insert Position

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

  4. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

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

  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(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  9. [LeetCode] 35. Search Insert Position ☆(丢失的数字)

    转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html    思路 Given a sorted array ...

  10. Java [leetcode 35]Search Insert Position

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

随机推荐

  1. Web.config中设置启用webservice远程调试访问

    在.NET 中已经默认将webservice的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在webservice的项目的中添web.config的<system ...

  2. 关于ios手机游览器针对overflow:hidden设置无效的解决办法

    Ordinarily, overflow: hidden; on the body tag is sufficient to prevent scrolling a web page, if for ...

  3. java创建自定义类的数组

    今天在学图论的最小生成树,开始一直在想是用邻接矩阵还是关联矩阵来表示图,但是发现这样都会有好多空间浪费.于是我就自定义一个边的类,里面包含了权值,关联的端点1,端点2,和图的表示字母.发现我想创建11 ...

  4. java自带的类压缩和下载,以及递归删除动态的文件(shiro项目中来的十)

    详见项目,不用借助于任何外在的jar包,通过jre自带的实现.

  5. MyISAM和InnoDB索引实现区别

    首先来讲MyISAM: MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.下图是MyISAM索引的原理图: 这里设表一共有三列,假设我们以Col1为主键,则上图是 ...

  6. Python Cookbook(第3版)中文版:15.16 不确定编码格式的C字符串

    15.16 不确定编码格式的C字符串¶ 问题¶ 你要在C和Python直接来回转换字符串,但是C中的编码格式并不确定. 例如,可能C中的数据期望是UTF-8,但是并没有强制它必须是. 你想编写代码来以 ...

  7. 在laravel环境下将图片存入MongoDB数据库

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  8. (luogu P4012)深海机器人问题 [TPLY]

    网页链接 https://www.luogu.org/problemnew/show/4012 做题背景 在不久的将来,人工智能发展使得人类大量失业,也使得现在的我们做[深海机器人问题]做得想死... ...

  9. 几道很Interesting的偏序问题

    若干道偏序问题(STL,分块) 找了4道题目 BZOJ陌上花开(权限题,提供洛谷链接) Cogs2479偏序 Cogs2580偏序II Cogs2639偏序++ 作为一个正常人,肯定先看三维偏序 做法 ...

  10. mac 配置虚拟主机

    http://www.upwqy.com/details/4.html 编辑httpd.conf文件,输入命令: vim /etc/apache2/httpd.conf 编辑httpd-vhosts. ...