描述: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

思路1:直观的想法,遍历数组,一旦当前元素>=target,当前元素的位置即为插入位置,边界问题处理一下即可。时间复杂度:O(n)

 class Solution {
public:
int searchInsert(int A[], int n, int target) { if(target > A[n-])
return n; else {
for(int i = ; i < n; i++) {
if(A[i] >= target)
return i;
}
}
}
};

思路2:由于给定数组为已经排好序的,可以使用二分查找,比较A[mid]与target的大小。时间复杂度:O(logn)

 class Solution {
public:
int searchInsert(int A[], int n, int target) { int start = ;
int end = n - ;
int mid = ; while(end >= start) { mid = (start + end) / ;
if(target == A[mid])
return mid;
else if(target > A[mid])
start = mid + ;
else
end = mid - ;
} if(target > A[mid])
return mid + ;
else
return mid; }
};

LeetCode Problem 35:Search Insert Position的更多相关文章

  1. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  2. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  4. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  5. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  6. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  7. LeetCode OJ 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. [array] leetcode - 35. Search Insert Position - Easy

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

  9. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

随机推荐

  1. @value

    通过name来获取bean对象. 这一点eclipse就没有intelliJ好,拉到方法内部上面就不现实方法名了,这里是上面调用的方法的内部. 处理内部属性: 然后就在这里抛出异常了: 我们打个deb ...

  2. mycat读写分离与主从切换

    1, 分库分表的优缺点.以及为什么分表方式无法成为主流? 分表:在台server上,长处是易维护,相似表分区.缺点是在一台dbserver上.无法分担IO.负载集中. 分库:在多台server上,长处 ...

  3. Python闭包的高级应用-装饰器的实现

    我们先看一个闭包的例子: from time import ctime def before_call(f): def wrapped(*args, **kargs): print 'before c ...

  4. 微信小程序 如何定义全局函数?

    微信小程序 定义全局数据.函数复用.模版等 微信小程序定义全局数据.函数复用.模版等问题总结: 1.如何定义全局数据 在app.js的App({})中定义的数据或函数都是全局的,在页面中可以通过var ...

  5. Taobao OpenERP Connector 简要说明

    Taobao OpenERP Connector 项目托管地址:https://github.com/buke/openerp-taobao 作者: wangbuke@gmail.com 功能: 1. ...

  6. ios图片轮播效果

    代码地址如下:http://www.demodashi.com/demo/11959.html ImageCarousel 简单封装的图片轮播器 内存过大由于我加载的图片分辨率较高(4k) 文件目录 ...

  7. hdu 5294 Tricks Device 最短路建图+最小割

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...

  8. android-pull方式解析xml文件以及XML文件的序列化

    android解析XML ---------------------------基础要像磐石 在android平台上可以使用SAX.DOM和自带的Pull解析器解析xml文件,本文主要介绍使用pull ...

  9. vs2017 生成代码策略 旧的 ObjectContext

    新版本的VS中已经去掉了生成ObjectContext的功能,需要手动下载一个生成ObjectContext的T4模板.在模型设计器的上下文菜单中选择添加代码生成项,在联机模板中选择对应EF版本的Ob ...

  10. Emmet:HTML/CSS编写插件

    http://www.iteye.com/news/27580 用法: http://docs.emmet.io/cheat-sheet/ sublime 2 添加:1. Ctrl+Alt+p -&g ...