给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
案例 1:
输入: [1,3,5,6], 5
输出: 2
案例 2:
输入: [1,3,5,6], 2
输出: 1
案例 3:
输入: [1,3,5,6], 7
输出: 4
案例 4:
输入: [1,3,5,6], 0
输出: 0
详见:https://leetcode.com/problems/search-insert-position/description/

Java实现:

class Solution {
public int searchInsert(int[] nums, int target) {
int size=nums.length;
if(size==0||nums==null){
return -1;
}
int l=0;
int r=size-1;
int m=0;
while(l<=r){
m=(l+r)>>1;
if(nums[m]==target){
return m;
}else if(nums[m]>target){
--r;
}else{
++l;
}
}
return l;
}
}

C++实现:

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int size=nums.size();
if(size==0||nums.empty())
{
return -1;
}
int l=0,r=size-1,m=0;
while(l<=r)
{
m=(l+r)>>1;
if(nums[m]==target)
{
return m;
}
else if(nums[m]>target)
{
r=m-1;
}
else
{
l=m+1;
}
}
return l;
}
};

035 Search Insert Position 搜索插入位置的更多相关文章

  1. lintcode:Search Insert Position 搜索插入位置

    题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...

  2. [LeetCode] 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】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  5. [LeetCode] 035. Search Insert Position (Medium) (C++)

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

  6. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the 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每天一题】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 寻找插入位置

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

随机推荐

  1. 为什么对象序列化要定义serialVersionUID

    对于实现了java.io.Serializable接口的实体类来说,往往都会手动声明serialVersionUID,因为只要你实现了序列化,java自己就会默认给实体类加上一个serialVersi ...

  2. java基础知识学习 java异常

    1: Unchecked Exception( 也就是运行时异常) VS  Check Exception(非运行时异常) 2: 运行期异常  VS  非运行期异常? 非运行时异常: 必须在代码中显示 ...

  3. Linux编程里getopt_long_only函数用法详解

    在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...

  4. mariadb的读写分离

    实验环境:CentOS7 设备:一台主数据库服务器,两台从数据库服务器,一台调度器 主从的数据库配置请查阅:http://www.cnblogs.com/wzhuo/p/7171757.html : ...

  5. Angular06 组件、模块、父子组件之间的数据传递

    1 创建组件 进入到angular项目的根目录,执行如下命令 ng g component test-component 注意:执行完上述命令后在angular项目的src/app文件夹下就会多出一个 ...

  6. 在64位ubuntu上安装alienbrain客户端

    一.首先从Alienbrain_EN_10.5.zip安装包(网上可搜索下载)里提取出linux版安装文件:Installations/Clients/Linux/NoVM/install.bin并c ...

  7. Coding 如何使用 Coding 开发 Coding

    Coding Anytime Anywhere Coding 团队有 70 多人,分布在全国各地(深圳,北京,上海,成都),我们使用 Coding 作为云端办公室,以云端协作的方式管理事务,文件等,我 ...

  8. linux 的有用的网站

    从windows下移到linux下还有很长的路走阿,慢慢记录一些有用的网站吧 http://www.yolinux.com/ http://linux.die.net/

  9. 高考是最后一次拼智商的事了。(beacuse 大多数人的努力程度之低根本轮不到拼天赋!)

    高考是最后一次拼智商的事. —因为大多数人的努力程度之低  根本轮不到拼天赋 在这个不起眼的小公司实习也有两周了,周四经理说说为了增加IOS开发小组和安卓开发小组之间的交流,准备每周开一次这种报告会. ...

  10. ADO.Net事务处理

    SQL Server中的事务可以将多个数据库增删改查操作合并为单个工作单元,在操作过程中任何部分出错都可以滚回已经执行的所有更改.ADO.Net中也提供了事务处理功能,通过ADO.net事务,可以将多 ...