Description:

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

基于数组的查找:

public class Solution {
public int searchInsert(int[] nums, int target) { if(target <= nums[0]) {
return 0;
} if(target > nums[nums.length-1]) {
return nums.length;
} for(int i=0; i<nums.length; i++) {
if(target <= nums[i]) {
return i;
} } return nums.length; }
}

LeetCode——Search Insert Position的更多相关文章

  1. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  2. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. [LeetCode] 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] Search Insert Position 二分搜索

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

  5. [LeetCode] 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 Search Insert Position Python

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

  7. LeetCode Search Insert Position (二分查找)

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

  8. [Leetcode] search insert position 寻找插入位置

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

  9. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

随机推荐

  1. java资料——线程(转)

    线程       线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程 ...

  2. 工作队列workqueue应用

    工作队列是另一种将工作推后执行的形式,它可以把工作交给一个内核线程去执行,这个下半部是在进程上下文中执行的,因此,它可以重新调度还有睡眠. 区分使用软中断/tasklet还是工作队列比较简单,如果推后 ...

  3. hadoop 参数

    看<Hadoop:权威指南>的时候收集了书上写的一些需要优化的参数,记录了一下子,给大家分享一下吧. 1.mapred.task.timeout 任务超时时间,默认是10分钟 2.mapr ...

  4. Node.js Streams:你需要知道的一切

    Node.js Streams:你需要知道的一切 图像来源 Node.js流以难以使用而闻名,甚至更难理解.好吧,我有个好消息 - 不再是这样了. 多年来,开发人员在那里创建了许多软件包,其唯一目的是 ...

  5. Fork of LGPL version of JPedal

    https://github.com/on-site/JPedal —————————————————————————————————————————————————————————————————— ...

  6. SQL Server 查询分析器键盘快捷方式

    下表列出 SQL Server 查询分析器提供的所有键盘快捷方式. 活动 快捷方式 书签:清除所有书签. CTRL-SHIFT-F2 书签:插入或删除书签(切换). CTRL+F2 书签:移动到下一个 ...

  7. tensorflow函数解析:Session.run和Tensor.eval的区别

    tensorflow函数解析:Session.run和Tensor.eval 翻译 2017年04月20日 15:05:50 标签: tensorflow / 机器学习 / 深度学习 / python ...

  8. 网络协议之bt---bt协议详解 DHT篇(下)

    -------------------------author:pkf -------------------------------qq:1327706646 ------------------- ...

  9. 关于解决用tutorial7教程中的代码打造一款自己的播放器中的声音噪音问题

    ////////////////////////////////////////////////////////////////////////////////////////////对于用FFMPE ...

  10. TaintDroid下载预编译(五):TaintDroid(Android)系统编译虚拟机和真机測试

    光说不练非好汉,如今就让我们开启自己编译的系统測试!事实上懂得这些过程.就知道了Android手机系统定制的整个流程.现有的智能机都是使用Google的android开源的系统然后加入一些自己的东西. ...