搜索插入位置
来源:互联网 发布:淘宝宝贝历史记录恢复 编辑:程序博客网 时间:2024/06/05 00:24
容易 搜索插入位置
28%
通过
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
您在真实的面试中是否遇到过这个题?Yes
样例
[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 static int searchInsert(int[] A, int target) { // write your code here int start = 0; int end = A.length-1; while(start<=end) { int mid = (start+end)/2; if(A[mid]<target) { start = mid+1; } else if(A[mid]>target) { end = mid-1; } else return mid; } return start; }}
0 0
- 题目:搜索插入位置
- LintCode : 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode ----搜索插入位置
- LintCode : 插入搜索位置
- 搜索插入位置
- Lintcode 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode:搜索插入位置
- Lintcode搜索插入位置
- lintCode 搜索插入位置
- LintCode_060_搜索插入位置
- 60. 搜索插入位置
- 【lintcode笔记】搜索插入位置
- 多线程:线程池
- RESTful API
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees(模拟)
- 链表反致
- 【C#】基础名词解释
- 搜索插入位置
- Hibernate-映射关系- 多对多关联关系
- 二维空间的创建与使用
- Docker容器资源管理cgroups功能测试
- bzoj3539【Usaco2014 Open】Odometer
- php图形处理
- 黑马程序员--反射(上)
- java数组的行数和列数
- 黑马程序员--反射(下)