一天一道LeetCode系列

(一)题目

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

(二)解题


/*

本题的思路是:如果目标数大于vector中的最大数,就返回vector的长度,即插在最后面

如果目标是小于vector中的最小数,就返回0,即插在最前面

如果在vector范围内,就用二分法查找,如果有等于target就返回其序号,如果没有即在i=j时退出,返回i表示target的插入位置

*/

class Solution {

public:

    int searchInsert(vector<int>& nums, int target) {

        int len = nums.size();

        if(target > nums[len-1]) return len;

        if(target < nums[0]) return 0;

        int i = 0;

        int j = len-1;

        while(i<=j)

        {

            int mid = (i+j)/2;

            if(nums[mid] == target) return mid;

            else if(nums[mid]>target) j=mid-1;

            else i = mid+1;

        }

        return i;

    }

};

【一天一道LeetCode】#35. 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] 35. 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

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  4. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

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

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

  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 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  9. [LeetCode] 35. Search Insert Position ☆(丢失的数字)

    转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html    思路 Given a sorted array ...

  10. Java [leetcode 35]Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

随机推荐

  1. 微信小程序基础之开源项目库汇总

    awesome-github-wechat-weapp 是由OpenDigg整理并维护的微信小程序开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. (链接:ht ...

  2. audio session config

    #pragma mark - #pragma mark - audio session config - (void)setAudioSessionConfig { NSError *error; A ...

  3. ARM C C++内存对齐

           ARM 系列处理器是 RISC (Reducded Instruction Set Computing)处理器.很多基于ARM的高效代码的程序设计策略都源于RISC 处理器.和很多 RI ...

  4. JavaScript基础精讲

    ---------------------------------------------------------------------------------------------------- ...

  5. [OpenCV]在显示窗口中截图

    [OpenCV]在显示窗口中截图 简介 介绍使用OpenCV实现简单的截图功能.首先阐述实现此功能的基本步骤,然后给出实现代码,最后贴出实验结果以及遇到的问题. 基本步骤 我们需要知道OpenCV使用 ...

  6. 将Gradle项目发布到Jcenter和Maven Central

    Jcenter和Maven Central 为了方便我们理解Android studio是如何帮助我们获取开源库的,我们需要理清几个概念.Apache Maven是Apache开发的一个工具,提供了用 ...

  7. IMDG产品功能扩展

    开源IMDG通常都提供了SPI或其他接口,供用户自行扩展.以Hazelcast为例,我们可以用一些好玩的小工具增强其查询.Map和后端持久化的功能.这些小工具虽然看起来很小,但功能也非常强大. SQL ...

  8. static修饰符详解

    static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static ...

  9. Servlet之HTTP状态码

    HTTP 请求和 HTTP 响应消息的格式是类似的,结构如下: 初始状态行 + 回车换行符(回车+换行) 零个或多个标题行+回车换行符 一个空白行,即回车换行符 一个可选的消息主体,比如文件.查询数据 ...

  10. Maven2插件开发入门

    一.创建Maven项目 首先创建一个Maven插件项目,可以手动或使用mvn archetype:create从原型创建.pom.xml配置如下: 1 2 3 4 5 6 7 8 9 10 11 12 ...