60.Search Insert Position.md
描述
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
您在真实的面试中是否遇到过这个题?
样例
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.
[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6],0 → 0
挑战
O(log(n)) time
public class Solution {
/**
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: An integer
*/
public int searchInsert(int[] A, int target) {
// write your code here
if(A.length == 0 ){
return 0;
}
if(target < A[0]){
return 0;
}
if(target > A[A.length-1]){
return A.length;
}
int left = 0;
int right = A.length-1;
while(left + 1 < right){
int mid = left + (right-left)/2;
if(A[mid] == target){
return mid;
}else if (A[mid] > target){
right = mid - 1;
} else if(A[mid] < target){
left = mid + 1;
}
}
//重点
//三种情况:
if(A[left] >= target) {
return left;
}
if(target<= A[right] && target > A[left]) {
return right;
}
return right+1;
}
}
60.Search Insert Position.md的更多相关文章
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- 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导致的溢 ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【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 ...
随机推荐
- cf862d 交互式二分
/* 二分搜索出一个01段或10即可 先用n个0确定1的个数num 然后测试区间[l,mid]是否全是0或全是1 如果是,则l=mid,否则r=mid,直到l+1==r 然后再测试l是1还是r是1 如 ...
- tomcat安装出现的闪退问题
如果闪退 在该文件中结尾添加pause 可以检测到路径问题是不是有问题
- 20165328 预备作业3 Linux安装及命令
Linux安装及学习 Linux安装遇到的问题: 问题:在我开始安装虚拟机的时候,在安装过程中总会出现初始界面,且无法跳过,陷入死循环. 解决方法:我在网上百度搜索该问题之后得到了答案,第一个界面是要 ...
- h5在手机端实现简单复制
<a href="https://blog-static.cnblogs.com/files/ruanqin/clipboard.min.js">下载clipborrd ...
- Echo()、print()、print_r()区别
echo可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用.echo是php的内部指令,不是函数, ...
- 社会单位消防安全户籍化管理系统——半自动提交V1.0版本
社会单位消防安全户籍化管理系统——半自动提交V1.0版本 首先先上代码,开发这个小程序其实是用来帮助同事完成一项每天都做的繁琐事件,以往需要花费十分钟做这件事情,现在就是傻瓜式,点几下鼠标就好了.本来 ...
- MVC AOP解决JsonResult返回json时间格式
新建JsonNetResult类:JsonResult public class JsonNetResult: JsonResult { public JsonNetResult() { Settin ...
- Nancy 返回值详解
简介 Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono.它的返回值也是多种多样的,适应各种不同的情况.包括Response.AsFile( ...
- CentOS6安装Zabbix4.0
安装依赖包yum install net-snmp-devel libevent-devel libxml2-devel curl-devel libjpeg-devel libpng-devel l ...
- ASP.NET Core IHostEnvironment和IApplicationLifetime介绍
IHostEnvironment获取程序信息 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app ...