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. Anaconda安装和环境的搭建

    Anaconda安装 在官网上下载最新的Anaconda https://www.anaconda.com/distribution/ 我使用的是2018.12,Python 3.7这个版本的. 安装 ...

  2. Java求字符串中出现次数最多的字符

    Java求字符串中出现次数最多的字符  [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51933611      Java ...

  3. python数据类型之字符串(str)和其常用方法

    字符串是有序的,不可变的. 下面的例子说明了字符串是不可变的 name = 'alex' name = 'Jack' """ 并没有变,只是给name开启了一块新内存,储 ...

  4. 装饰器与lambda

    装饰器 实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉.只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番, ...

  5. SpringBoot接收前端参数的三种方法

    都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码: @RestController public class ControllerTest { //访问 ...

  6. luogu2756 飞行员配对方案问题

    匈牙利 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  7. [python][oldboy] * **的用法

    * 和**主要用在函数的参数中, # coding=utf8 """ 三种编码: 1 python程序代码的编码 # coding=utf8 2 设置/查看python程 ...

  8. ibatis 动态SQL

    直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...

  9. OA笔记

    一:Asp.Net MVC请求处理原理(Asp.Net mvc 是怎样进入请求管道的.)请求-->IIS--->ISAPIRuntime-->HttpWorkRequest--> ...

  10. ThinkPHP5杂技(二)

    不要使用数据库查询嵌套 if (!$listA = Db::name('coin') ->field('id,symbol') ->where('id', 'IN', logic('All ...