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.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0


题解:简单的二分搜索,直接放代码:

JAVA版本:

 public int searchInsert(int[] A, int target) {
int answer = 0;
int front = 0;
int end = A.length-1;
while(front <= end){
int mid = (front + end)/2;
if(A[mid] == target)
return mid;
if(A[mid] < target)
front = mid + 1;
else
end = mid-1;
}
return front;
}

【leetcode刷提笔记】Search Insert Position的更多相关文章

  1. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  2. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  3. 【leetcode刷题笔记】Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. LeetCode Arrary Easy 35. Search Insert Position 题解

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

  5. leetcode 刷道题 70 earch 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 ...

  7. LeetCode记录之35——Search Insert Position

    这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...

  8. 【leetcode刷提笔记】Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. 【leetcode刷提笔记】Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

随机推荐

  1. php 获取当前域名

    #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br> ...

  2. 第一百九十二节,jQuery EasyUI 使用

    jQuery EasyUI 使用 学习要点: 1.引入必要的文件 2.加载 UI 组件的方式 3.使用 easyload.js 智能加载 4.Parser 解析器 本节课重点了解 EasyUI 的两种 ...

  3. Hibernate每个子类一张表(使用XML文件)实例

    在每个子类一张表的情况下,子类映射表与主键和外键关系与父类映射表相关. 类的<joined-subclass>元素用于使用主键和外键关系将子类与父对象进行映射. 在这个例子中,我们将使用h ...

  4. Spring MVC多项单选按钮

    以下示例显示如何在使用Spring Web MVC框架的表单中使用多选按钮(RadioButton).首先使用Eclipse IDE来创建一个WEB工程,实现一个让用户可选择自己喜欢的数字的功能.并按 ...

  5. 004androidStudio ndk开发环境

    004androidStudio ndk开发环境 android studio中编译C/C++源代码 1. 配置ndk.dir 在 local.properties 添加如下配置: sdk.dir=p ...

  6. mongodb性能问题及原理分析

    近期忙着把一个项目从MySQL迁移到MongoDB,在导入旧数据的过程中.遇到了些许波折,犯了不少错误,但同一时候也学到了不少知识,遂记录下来. 公司为这个项目专门配备了几台高性能务器,清一色的双路四 ...

  7. 求伪逆矩阵c++代码(Eigen库)

    非方阵的矩阵的逆矩阵  pseudoInverse 伪逆矩阵是逆矩阵的广义形式,广义逆矩阵 matlab中是pinv(A)-->inv(A). #include "stdafx.h&q ...

  8. 如何通过Mac 下的SVN拉取代码

    背景:今天入职了一家新单位,用的svn,我之前一直用的win下的git和svn,然后我现在用自己的mac开发,所以有了标题的疑问 博文由来:看了几个博客写的都很繁琐,看半天才能解决我的疑问,所以自己写 ...

  9. cocos2d-x之读开发技术精解

    引擎位置(依次往下): 游戏App->逻辑与规则->引擎->运行的平台->硬件接口(驱动运行库API) 1. 渲染框架 CCNode绘制基类(引擎核心类都继承于它,形成一个链表 ...

  10. 用sql语句,快速备份表数据

    1.SqlServer数据库 --DataTable 原数据表 --DataTable_20150717 要备份的新表名 select * into DataTable_20150717 from D ...