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

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 4:

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

//mid+1,mid-1,是为了防止有重复数据,可能死循环
public int searchInsert(int[] A, int target) {
int low = 0, high = A.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (A[mid] == target)
return mid;
else if (A[mid] > target)
high = mid - 1;
else
low = mid + 1;
}
return low;
}

[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 (搜索嵌入的位置)

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

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

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

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

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

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

  9. 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. c++标准库多线程入门

    从c++ 11开始,语言核心和标准库开始引入了对多线程的原生支持.如下所示: int doSth(char c) { default_random_engine dre(c); uniform_int ...

  2. 阿里druid连接池

    1.加入jar包, 下载地址:druid-1.1.0.zip 2.ApplicationContext.xml <!-- druid阿里云连接池 --> <bean name=&qu ...

  3. 三步搞定 opencv 初始环境设定

    一.设定bin的初始位置:比如我的电脑 D:\安装程序\opencv\build\x86\vc10\bin      H:\生产力工具\opencv\build\x86\vc10\bin D:\安装程 ...

  4. 20165310_java_blog_week1.md

    2165310 <Java程序设计>第1周学习总结 教材学习内容总结 了解Java基本配置环境 学习Java命名.编译.运行规则 拓展学习包的编译.运行 熟悉Git的运用方式 教材学习中的 ...

  5. TP/TCP/UDP

    这两周我继续学习CCSDS协议栈中位于传输层较低位置的SCPS-TP协议,并且复习了TCP/IP体系中的TCP协议和UDP协议,通过学习和对比两个体系的协议,加深了我对SCPS-TP协议的认识和理解. ...

  6. 探索Java8:(三)Predicate接口的使用

    上一篇学习了下Function接口的使用,本篇我们学习下另一个实用的函数式接口Predicate. Predicate的源码跟Function的很像,我们可以对比这两个来分析下.直接上Predicat ...

  7. uva 1658 Admiral - 费用流

    vjudge传送门[here] 题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小. 正解是吧2 ...

  8. python函数作用域LEGB

    我们的在学习Python函数的时候,经常会遇到很多定义域的问题,全部变量,内部变量,内部嵌入的函数,等等,Python是如何查找的呢?以及Python又是按照什么顺序来查找的呢?这里做一个顺序的说明 ...

  9. tslib移植笔记(1)【转】

    本文转载自:https://blog.csdn.net/zijie_xiao/article/details/50740950 tslib移植笔记(1)2016-04-25 tslib背景[摘自百度] ...

  10. 【异常记录(11)】 Web应用程序项目 已配置为使用 IIS。无法访问 元数据库。您没有足够的特权访问计算机上的 IIS 网站

    解决办法: 1.项目上右键, 编辑 xxxx.csproj 2.找到  <UseIIS> 标签, 改为 <UseIIS>False</UseIIS> 3.右键,重新 ...