搜索插入位置
来源:互联网 发布:python 代码鉴赏 编辑:程序博客网 时间:2024/06/07 05:01
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6],0 → 0
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) { if(A==null||A.length==0){ return 0; } int start=0; int end=A.length-1; int mid; while(start+1<end){ mid=start+(end-start)/2; if(A[mid]==target){ return mid; }else if(A[mid]<target){ start=mid; }else{ end=mid; } } if(A[start]>=target){ return start; }else if(A[end]>=target){ return end; }else{ return end+1; } }}
阅读全文
0 0
- 题目:搜索插入位置
- LintCode : 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode ----搜索插入位置
- LintCode : 插入搜索位置
- 搜索插入位置
- Lintcode 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode:搜索插入位置
- Lintcode搜索插入位置
- lintCode 搜索插入位置
- LintCode_060_搜索插入位置
- 60. 搜索插入位置
- 【lintcode笔记】搜索插入位置
- WinCacheGrind +Xdebug查看调试流程结果
- SDKD 2017 Summer Single Training #02 (训练赛2)
- 解决Ubuntu 16.04 搜狗输入法 只有悬浮窗,不能输入中文的问题
- 回调函数
- Android studio之Unknown run configuration type AndroidRunConfigurationType解决办法
- 搜索插入位置
- Hibernate一级缓存
- 剑指Offer——二叉树的镜像
- 最小的ROS移动机器人ROM有多小(转自微信公众号“机器人软件研究”)
- Etcd介绍
- [LeetCode] 206. Reverse Linked List
- 从kmp到ac自动机
- kylin系列教程(三)——访问方式
- JAVA 攻城狮 第七天