LeetCode Problem 35:Search Insert Position
描述: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的更多相关文章
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- 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 ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 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 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 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导致的溢 ...
随机推荐
- 学习ajax总结
之前公司的ajax学习分享,做一点总结,加深记忆 什么是ajax? 异步的的js和xml,用js异步形式操作xml,工作主要是数据交互 借阅用户操作时间,减少数据请求,可以无刷新请求数据 创建一个对象 ...
- C++11 新特性之 变长參数模板
template <typename ... ARGS> void fun(ARGS ... args) 首先明白几个概念 1,模板參数包(template parameter pack) ...
- Eclipse 一直Building Workspace 的解决办法
Eclipse 一直不停 building workspace完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭 3.maven下载lib挂起 等.. 二.解决总结 (1).解决 ...
- 命名空间System.Configuration中不存在类型或命名空间名称ConfigurationManager
C#连接数据库时.这是个非经常见的错误,我刚開始就直接using System.Configuration;还是没能解决,真相是要手动加入这个程序集的引用,在项目右键加入引用选择System.Conf ...
- 安装JDK不当--找不到或无法加载主类 com.sun.tools.javac.Main
问题描述:我的问题是在使用javac编译测试程序市,出现如下错误: 错误: 找不到或无法加载主类 com.sun.tools.javac.Main 解决办法:当出现这个错误时,百度之,结果很多人都是说 ...
- Android BlueDroid(三):BlueDroid蓝牙开启过程enable
关键词:bluedroid enableNative BTIF_TASK BTU_TASK bt_hc_work_thread set_power preload GKI作者:xubin3417 ...
- nginx缓存设置
http://linux008.blog.51cto.com/2837805/547236 目的:缓存nginx服务器的静态文件.如css,js,htm,html,jpg,gif,png,flv,sw ...
- Centos使用光盘作为本地yum源
[root@localhost CentOS]# mkdir /media/CentOS把光盘加载到本地[root@localhost CentOS]# mount /dev/cdrom /media ...
- Android解决button反复点击问题
public class BaseActivity extends Activity { protected boolean isDestroy; //防止反复点击设置的标志.涉及到点击打开其它Act ...
- seo-mask -- 为单页应用创建一个适合蜘蛛爬取的seo网站
seo-mask seo-mask是利用搜索引擎蜘蛛的爬取原理(蜘蛛只会爬取网页的内容,并不会关心解析网页里的css和js),制作一套专门针对seo的镜像网站,鄙人称它为针对seo的mask,让蜘蛛看 ...