索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


035. Search Insert Position (Medium)

链接

题目:https://leetcode.com/problems/search-insert-position/

代码(github):https://github.com/illuz/leetcode

题意

要把一个数有序插入到一个有序数组里,问插入的位置。

分析

还是二分变形题。

  1. 用 STL 的 lower_bound 偷懒。
  2. 二分,最后注意推断一下是否找到。要输出什么。

代码

C++:

class Solution {
public:
int searchInsert(int A[], int n, int target) {
// if use lower_bound
// return lower_bound(A, A + n, target) - A; int l = 0, r = n - 1, mid = 0;
while (l < r) {
mid = l + (r - l) / 2;
// mid = (l + r) / 2;
if (A[mid] < target)
l = mid + 1;
else
r = mid;
}
return A[l] < target ? l + 1 : l;
}
};

[LeetCode] 035. Search Insert Position (Medium) (C++)的更多相关文章

  1. LeetCode 035 Search Insert Position

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

  2. Java for LeetCode 035 Search Insert Position

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

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  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. v形 加强版

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  2. Python批量下载电视剧电影--自己动手丰衣足食

    前言 为了看美剧<天蝎>,在电影天堂找到了,于是就想下载下来好好欣赏. 废话不说了,直接上代码. 代码 import requests,re,os,time url = "htt ...

  3. [Windows Server 2012] SQL Server 备份和还原方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:SQL S ...

  4. 16位/32位/64位CPU的位究竟是说啥

    平时,我们谈论CPU,都会说某程序是32位编译,可以跑在32位机或64位机,或则是在下载某些开源包时,也分32位CPU版本或64CPU位版本,又或者在看计算机组成相关书籍时,特别时谈到X86 CPU时 ...

  5. 01Hypertext Preprocessor

    Hypertext Preprocessor PHP即Hypertext Preprocessor是一种被广泛使用的开放源代码多用途动态交互性站点的强有力的服务器端脚本语言尤其适用于 Web开发人员可 ...

  6. 使用lombok注解@Getter @Setter方法代码编译成功,但是没有生成get,set方法

    现象描述: 在对应类对象中,添加lombok的@Getter或@Setter注解,编译没有问题,但是在使用类对象时,没有出现对应的get或set方法. 配置且编译ok,但是没有对应的get或set方法 ...

  7. 解决docker容器启动时候无法映射端口的问题

    当我们停止防火墙后,docker容器启动映射端口可能无法映射端口,这个时候需要重建docker0网桥. 详细的错误是这样的: docker: Error response from daemon: d ...

  8. 关于mybatis返回值resultType为空的问题

    假设数据库中一个user表 此时只有id为1的数据,当我们查询id为2的年龄时的时候返回值为null 但是在mybatis中预定义UserMapper.xml中 <select id=" ...

  9. fork 系统调用

    对自己知识储备的感觉就是过于肤浅,很多东西知其名后就不了了之 此系列博客将记录进程分析的学习过程,希望能够多些深度 提到进程,最容易的想到就是fork系统调用,比较好和快速的找到的fork的相关信息就 ...

  10. sublime 使用笔记

    unbuntu安装sublime---------------------------------------------sudo add-apt-repository ppa:webupd8team ...