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 ...
随机推荐
- Android Studio 设置编辑器(Editor)的字体、字体大小
操作系统:Windows 10 x64 IDE:Android Studio 3.2.1 参考:https://www.cnblogs.com/diyishijian/p/6824328.html 备 ...
- ajax--参数默认值问题
注:通过参数默认值,能让参数映射更加灵活,有些参数可以不必传递,如果传递则 覆盖默认.并且永远都是后面的覆盖前面的内容 通过$.extend合并对象 语法1: var newobj= $.extend ...
- C++ friend友元函数和友元类(转)
一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有本类中的函数可以访问本类的 private 成员.现在,我们来介绍一种例外情 ...
- JAVA 代码中使用中文的办法
在编译代码中插入 -encoding UTF-8 示例: javac -encoding UTF-8 *.java
- install memcached for ubuntu
Memcached安装 1.先下载安装libevent 安装 libevent# tar zxvf libevent-1.4.9-stable.tar.gz# cd libevent-1.4.9-st ...
- PAT Basic 1069. 微博转发抽奖(20)
小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...
- MyBatis - 8.MyBatis工作原理
Mybatis 配置 1.SQLSessionFactory的初始化 根据配置文件获取 SqlSessionFactory 2.openSession获取SqlSession对象 3.getMappe ...
- Quartz.NET(任务调度)与Topshelf(服务)的综合使用
http://www.cnblogs.com/jys509/p/4628926.html http://cron.qqe2.com/ Quartz_Topshlf_Demo.7z
- 一脸懵逼学习Hive(数据仓库基础构架)
Hive是什么?其体系结构简介*Hive的安装与管理*HiveQL数据类型,表以及表的操作*HiveQL查询数据***Hive的Java客户端** Hive的自定义函数UDF* 1:什么是Hive(一 ...
- WPF在XAML中实现持续动画的暂停、恢复、停止
1.动画通过EventTrigger监听按钮的FrameworkElement.Loaded事件,但控件载入时就进行动画, 持续动画通过<BeginStoryboard Name="y ...