索引:[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. Objective-C设计模式——生成器Builder(对象创建)

    生成器 生成器,也成为建造者模式,同样是创建对象时的设计模式.该模式下有一个Director(指挥者),客户端知道该类引用用来创建产品.还有一个Builder(建造者),建造者知道具体创建对象的细节. ...

  2. IPython、Notebook、qtconsole使用教程

    IPython.Notebook.qtconsole使用教程 上一篇为Python,IPython,qtconsole,Notebook,Jupyter快速安装教程 1. 使用IPython 自动补全 ...

  3. 内存溢出及Jvm监控工具

    内存泄露与内存溢出 内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory. 内存泄露 memory leak,是指程序在申请内存后,无 ...

  4. Flask Web 发送邮件单文件

    import os from flask import Flask, render_template, session, redirect, url_for from flask_script imp ...

  5. oracle创建临时表空间、用户表空间、创建用户关联表空间、授权等

    1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_temp TEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test ...

  6. JS高级——弹出框的美化

    替换原有的alert方法,window.alert=function(){} https://blog.csdn.net/kirsten_z/article/details/76242286 http ...

  7. Fiddler—重复发送一个请求的设置

    https://jingyan.baidu.com/article/b2c186c829a85dc46ff6ff60.html 选中一个request——>Reissue Sequentaill ...

  8. mac下iterm2 设置笔记

    1.利用brew install zsh 来安装oh my zsh 2.chsh -s /bin/zsh,修改~/.zshrc文件 alias cls='clear' alias ll='ls -l' ...

  9. Linux命令运行监测和软件安装

    监测命令的运行时间 time command $ time sleep 5 real 0m5.003s # 程序开始至结束的时间,包括其它进程占用的时间片和IO时间 user 0m0.001s # 进 ...

  10. Python爬虫:抓取手机APP的数据

    摘要 大多数APP里面返回的是json格式数据,或者一堆加密过的数据 .这里以超级课程表APP为例,抓取超级课程表里用户发的话题. 1.抓取APP数据包 表单: 表单中包括了用户名和密码,当然都是加密 ...