搜索插入位置
来源:互联网 发布:linux应用技术 编辑:程序博客网 时间:2024/05/23 15:34
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例
[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) { // write your code here if(A.length == 0) return 0;int low = 0;int high = A.length-1;while(low<=high){int mid = (low +high)/2;if(target == A[mid])return mid;else if (target < A[mid]) {high = mid -1;}else {low = mid + 1;}}int i = 0;for(i=low;i<A.length;i++)if(target < A[i])break;return i; }}
0 0
- 题目:搜索插入位置
- LintCode : 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode ----搜索插入位置
- LintCode : 插入搜索位置
- 搜索插入位置
- Lintcode 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode:搜索插入位置
- Lintcode搜索插入位置
- lintCode 搜索插入位置
- LintCode_060_搜索插入位置
- 60. 搜索插入位置
- 【lintcode笔记】搜索插入位置
- 《java多线程编程核心技术》之线程间通信
- java代码调用数据库的存储过程和函数 CallableStatement的用法
- reinterpret_cast<T>() static_cast<T>() const_cast<T>() dynamic_cast<T>()
- Spring中的国际化
- 不同APP通过SharedPreferences传递数据(共享数据)
- 搜索插入位置
- 你必须了解有关Babel 6的6件事
- 算法提高 学霸的迷宫 (题解)
- Bulls and Cows
- How is deep learning different from multilayer perceptron?
- linux 逻辑卷管理
- 看完linux课本第五章后的一点总结,顺便做个报告。
- RecyclerView实现底部加载更多功能
- [若有所悟]七步,改变你的纯文本报告面目