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. jquery - 动态绑定事件

    举个例子: html页面 <div><button type="button" class="test">测试</button&g ...

  2. Python导入不同文件夹下模块

    import os.path as osp import sys def add_path(path): if path not in sys.path: sys.path.insert(0, pat ...

  3. 设置内容 - text()、html() 以及 val()

    我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回表单字段的 ...

  4. python 字符串格式化 ( 百分号 & format )

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. ----百分号 tpl = ...

  5. SUSE10 SP4源码升级Python到2.6.6

    1.安装依赖包(CentOS可采用yum) zypper in gcc gcc-c++ openssl-devel-32bit openssl-devel readline-devel readlin ...

  6. android Material design是什么

    Material design概述: Material design是一套UI样式标准,应该会提供一些新的API这写API包含了以下五大模块内容,分别是: Material Theme New Wid ...

  7. HTML学习笔记——语法+骨架

    一.什么是HTML HTML是用来制作网页的标记语言 HTML是Hypertext Markup Language的英文缩写,即超文本标记语言 HTML语言是一种标记语言,不需要编译,直接由浏览器执行 ...

  8. Kotlin——初级篇(二):变量、常量、注释

    在Kotlin中的变量.常量以及注释多多少少和Java语言是有着不同之处的.不管是变量.常量的定义方式,还是注释的使用.下面详细的介绍Kotlin中的变量.常量.注释的使用.以及和Java的对比. 如 ...

  9. code first 数据库无损迁移

    环境:vs2013+nuget Enable-Migrations -EnableAutomaticMigrations Update-Database

  10. api xml database 设计一种数据库

    w 问题 0-新增和读取,可以忽略更新和删除: 1-被请求方的xml dom结构多层且不定,且未来可能增删某些键(dom节点),且键值长度最值可能无法确定: 3-请求过程可能出现异常exception ...