Question:

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

Answer

class Solution {
public int searchInsert(int[] nums, int target) { int left = 0;
int right = nums.length-1; while(left<right)
{
int mid = (left + right)/2;
if(nums[mid]>target)
{
right = mid - 1;
}
else if(nums[mid]<target)
{
left = mid + 1;
}
else
{
return mid;
}
} if(nums[left]<target)
{
return left+1;
}
else
{
return left;
} }
}

效率:

Leetcode练习题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] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  3. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

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

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

随机推荐

  1. Linux中crontab定时任务

    crontab安装(centOS) yum -y install vixie-cron crontab语法(计划任务) crontab [-u user] file crontab [-u user] ...

  2. Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt

    最近安装python,已经安装好,cmd终端中输入python.pip等命令都有用 然而在配置requirements.txt文件过程中,执行语句 “pip install -r requiremen ...

  3. go语言之指针

    package main import "fmt" //指针 //go语言的指针是非常容易学习的,比c中容易很多,他可以更简单的执行一些任务 //与变量类型,使用前需要定义 fun ...

  4. PAT 1006 Sign In and Sign Out 查找元素

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  5. 如何解决eclipse远程服务器上面的Rabbitmq连接超时问题?

    1.嗯,问题呢,就是一开始安装好RabbitMQ,练习了一下RabbitMQ的使用,但是呢,过了一段时间,我来复习的时候,发现运行出现下面的错误了.后来想想,是自己学习微服务的时候,修改了/etc/h ...

  6. C# 从图片中截取一部分图片,并返回所截取的图片

    /// <summary> /// 从图片中截取一部分图片 /// </summary> /// <param name="fromImagePath" ...

  7. Java生鲜电商平台-商家支付系统与对账系统架构实战

    Java生鲜电商平台-商家支付系统与对账系统架构实战 说明:关于生鲜电商平台,支付系统是连接消费者.商家(或平台)和金融机构的桥梁,管理支付数据,调用第三方支付平台接口,记录支付信息(对应订单号,支付 ...

  8. 剑指offer笔记面试题14----剪绳子

    题目:给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,n > 1 并且m > 1),每段绳子的长度记为k[0], k[1], ...k[m].请问k[0] x k[1] x .. ...

  9. NoiseSystem数据库设计心得-洋芋好想飞

    团队:洋芋好想飞 成员:乔祥硕 石高飞 杨慧慧 梁家豪 潘景渝 整理:乔祥硕 PM乔祥硕: 10月25日14:30到17:30,10月27日14:30到17:30,11月1日14:30到17:30,这 ...

  10. JS中的slice()和splice()的区别以及记忆方式

    总结 splice()会改变原来的数组,返回的是被改变的内容,比如说通过splice删掉了某一项,那么返回的是删掉的这一项,当然还是会以数组的形式返回. slice不会对原数组进行改变,会返回一个新的 ...