【LintCode】Search Insert Position 搜索插入位置
来源:互联网 发布:java 间隔1秒执行一次 编辑:程序博客网 时间:2024/05/20 04:48
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例
[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(null == A) return -1; if(A.length == 0) return 0; int l = 0; int r = A.length - 1; while(l <= r) { int m = l + (r - l)/2; if(A[m] == target) { return m; } else if(A[m] < target) { l = m + 1; } else { r = m - 1; } } return l; }}
0 0
- 【LintCode】Search Insert Position 搜索插入位置
- 搜索插入位置:Search Insert Position
- Search Insert Position 插入位置
- 60.Search Insert Position-搜索插入位置(容易题)
- 【LeetCode】Search Insert Position搜索插入位置[数组、二分法]
- leetcode 35. Search Insert Position搜索插入位置(二分查找)
- LeetCode Search Insert Position查找插入位置
- Search Insert Position 查找插入的位置
- Search Insert Position 寻找插入的位置
- 35. Search Insert Position 查找插入位置
- [Lintcode] Search Insert Position
- leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法
- 【LeetCode-面试算法经典-Java实现】【035-Search Insert Position(搜索插入位置)】
- Search Insert Position 找适合插入的位置@LeetCode
- 寻找序列的插入位置 Search Insert Position
- 【LeetCode】- Search Insert Position(查找插入的位置)
- LeetCode | Search Insert Position(查找插入位置)
- [Leetcode] Search Insert Position 寻找插入的位置
- 自己动手写MutableArray
- FFmpeg参数
- addClass
- HDU1032 The 3n + 1 problem
- 为什么静态类不能访问非静态类
- 【LintCode】Search Insert Position 搜索插入位置
- 第五天的笔记
- C语言基础6
- ffmpeg的tutorial中文版学习笔记(四)(新版函数额外补充)
- 建立平衡二叉树+层次遍历
- ios即时通讯客户端开发之-mac上安装MySQL
- 小兔的棋盘(hdu2067)
- linux下mysql的root密码忘记解决方法
- ffmpeg的tutorial中文版学习笔记(五)