转载: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. Java 实现后缀xls文件读取

    Java 实现后缀xls文件读取 一.开发环境 poi采用的3.9版本 + JDK1.6 + Myeclipse 二,JAR包 三.实现代码 实体类:UserRoleBean package nc.x ...

  2. c++性能之对象与指针性能比较、以及java与c++性能对比实测

    为了更加直观的比较,好吧,我们选择以对象的初始化并add到list为例子. 首先,定义object如下: #include <string> #pragma once using name ...

  3. 散列表(HashTable)

    散列表 i. 散列函数 i. 冲突解决 ii. 分离链表法 ii. 开放地址法 iii. 线性探测法 iii. 平方探测法 iii. 双散列 ii. 再散列 ii. 可扩散列 i. 装填因子:元素个数 ...

  4. 立几个flag

    有时候会心血来潮想学一点东西,然后搞别的东西的时候就慢慢忘了.. 这里做个备忘录: 树分块/树上莫队 广义后缀自动机(大概这辈子都不会去学了) 带花树(如果我能学的动那个线代的随机算法就放弃这个) 模 ...

  5. 基于Android的闹钟的软件

    一.本课题要求:设计一个基于Android的闹钟的软件. 实现的功能有:能通过界面设置闹钟的启动条件建立后台服务进程,当满足触发条件时,闹钟响应相应事件. 二.需求分析 该课题实现在手机操作系统And ...

  6. SPOJ Prime or Not - 快速乘 - 快速幂

    Given the number, you are to answer the question: "Is it prime?" Solutions to this problem ...

  7. MVC ---- T4模板的小练习

    1.先建立两个模板文件 :Manger.ttinclude.DBHelper.ttinclude Manger.ttinclude <#@ assembly name="System. ...

  8. 一些WGS健康体检网站和公司

    1.wegen https://www.wegene.com/ 2.阅己基因 http://www.selfgene.com/

  9. 【Django】【六】接口自动化测试框架

    我的源码地址:https://github.com/woshixiaoyu202017/djangoTest 详细构建步骤如下 1. 生成新的测试数据库的数据库表结构guest_test 2. 数据库 ...

  10. shell wc命令 统计行数

    users文件内容 hello world 我们要统计 users 文件的行数,执行以下命令: $ wc -l users users 也可以将输入重定向到 users 文件: $ wc -l < ...