https://oj.leetcode.com/problems/search-insert-position/

数组有序,给一个数,看它是否在数组内,如果是则返回位置,如果不在则返回插入位置。

因为原数组有序,所以使用二分查找。

注意:如果是要插入这个元素的话,当target元素小于begin时候,是在begin位置上插入,如果大于则在begin+1位置上插入。

要测试数组为空,或者只有一个元素时候情况。

#include <iostream>
using namespace std; class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(n==)
return ;
int begin = ;
int end = n-;
int middle = (begin + end)/;
while(begin<end)
{
if(target == A[middle])
return middle;
else if(target<A[middle])
end = middle - ;
else
begin = middle+;
middle = (begin + end)/;
}
if(A[begin] >= target )
return begin;
return begin +; }
}; int main()
{
int arr[] = {,,,};
Solution myS;
cout<<myS.searchInsert(arr,,);
}

LeetCode OJ--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] 035. Search Insert Position (Medium) (C++)

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

  3. LeetCode 035 Search Insert Position

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

  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 【 Search Insert Position 】python 实现

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

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

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

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

  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] 35. Search Insert Position 解决思路

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

  10. 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. 《Java并发编程实战》读书笔记一 -- 简介

    <Java并发编程实战>读书笔记一 -- 简介 并发的历史 并发的历史,也是人类利用有限的资源去提高生产效率的一个的例子. 设想现在有台计算机,这台计算机具有以下的资源: 单核CPU一个 ...

  2. python爬虫基础10-selenium大全4/8-Webelement

    Selenium笔记(4)Webelement 本文集链接:https://www.jianshu.com/nb/25338984 这是通过find方法找到的页面元素,此对象提供了多种方法,让我们可以 ...

  3. shell-code-拷贝文件

    #!/bin/bash while read F do cp ${F}"_pe_1.fastq.gz" /public/home/chenjy/usr/ZD/data/cleand ...

  4. usb hub 设备流程图

    在此处负责而来:http://blog.csdn.net/xuelin273/article/details/38646851  下面的转载于:http://blog.csdn.net/qianguo ...

  5. SSAS——MDX基础

    一.基本概念 MDX:一种查询语言,从多维的数据集单元格中检索数据.支持两种不同的模式: 1.表达式语言:定义和操纵Analysis Services对象和数据以计算值 2.查询语言:从Analysi ...

  6. 1507: [NOI2003]Editor(块状链表)

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4157  Solved: 1677[Submit][Stat ...

  7. IDEA-常用插件,使用FindBugs寻找bug,代码分析

    bug无处不在,但是我们总希望少一点bug. 最近发现了一款好用的寻找bug的插件,特此记下. 一.安装 路径:File-->Settings-->Plugins-->Browse ...

  8. install and config redis on ubuntu14.04

    1.installation: (1)download redis from http://redis.io/download (2)installation: $ tar -xvf redis-3. ...

  9. andorid studio 环境搭建

    1 安装jdk,配置jdk的环境变量http://www.cnblogs.com/liuhongfeng/p/4177568.html(通过java ,javac, java -version来察看j ...

  10. 【bzoj1907】树的路径覆盖 树形dp

    题目描述 输入 输出 样例输入 1 7 1 2 2 3 2 4 4 6 5 6 6 7 样例输出 3 题解 树形dp 设f[x]表示以x为根的子树完成路径覆盖,且x为某条路径的一端(可以向上延伸)的最 ...