给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
案例 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. 问题:ExecuteNonQuery 与 ExecuteScalar 结果: ExecuteNonQuery方法和ExecuteScalar方法的区别

    ExecuteNonQuery方法和ExecuteScalar方法的区别 ----ExecuteNonQuery():执行命令对象的SQL语句,返回一个int类型变量,如果SQL语句是对数据库的记录进 ...

  2. Material使用05 MdListModule模块 MdButtonToggleModule模块

    1 在共享模块中导入MdListModule模块 import { NgModule } from '@angular/core'; import { CommonModule } from '@an ...

  3. CF1042E Vasya and Magic Matrix

    感觉不会期望. 首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$. 下文中的下标均为排序后的下标. 这样子我们就可以推出公式: $f_i ...

  4. iOS开发,在main thread以外的thread更新UI

    如果需要在异步任务(Async Task)中更新UI,若直接设置UI,会导致程序崩溃. 例如,在异步block中去更改UI: NSOperationQueue *queue=[[NSOperation ...

  5. Entity Framework Code-First(10.3):Property Mappings

    Property Mappings using Fluent API: Here, we will learn how to configure properties of an entity cla ...

  6. 8. CTF综合靶机渗透(一)

    靶机说明 虚拟机难度中等,使用ubuntu(32位),其他软件包有: PHP apache MySQL 目标 Boot to root:从Web应用程序进入虚拟机,并获得root权限. 运行环境 靶机 ...

  7. xilinx planahead partial reconfiguration

    1.为什么要使用reconfiguration? reconfiguration,即逻辑可重配,指的是FPGA的逻辑只加载指定区域(功能)的逻辑,而对除此之外的逻辑不产生影响,最常用的就是PCIe/P ...

  8. .net 前端传值和后端接收的几种方式

    第一种:GET传参(常用): get传参方式就是链接?后写上参数和数据用&拼接. 第二种:POST传参(常用): 这种传参方式可以GET POST同时传,在链接上加参数后台用get方式接收,P ...

  9. ASP.NET MVC实现layui富文本编辑器应用

    先看看视图层 在视图层,使用的是视图助手--HtmlHelper,代替我们网页中传统的表单标签元素,其中的m代表实体模型.通过视图助手,为我们生成id和name属性相同的textarea标签. 备注: ...

  10. JSONCPP介绍

    描述JSON串 如何使用jsoncpp提供的数据结构来存储如下JSON串? // Configuration options { // Default encoding for text " ...