二分查找——Search Insert Position
来源:互联网 发布:阿里云服务器ssh登录 编辑:程序博客网 时间:2024/05/01 20:33
题目:
给定一个排序的数组和一个 target,若target在数组中找到,则返回下标,否则找出target要插入的index。
例如:
int[] array={1,3,5,6}; 5->2; 2 ->1; 7->4; 0->0.
值的注意的是,跳出while后的不同情况的判断。
public int serchInsert(int[] data,int target){if(data == null||data.length == 0)return 0;int left=0;int right=data.length-1;while(left < right-1){int mid=left+(right-left)/2;if(data[mid] == target)return mid;else if(data[mid] < target)left=mid;elseright=mid;}if(data[left] >= target)//!return left;if(data[right] >= target)return right;else //data[right]<targetreturn data.length;}
1 0
- 二分查找——Search Insert Position
- leetcode——Search Insert Position 二分查找 binary search
- Leetcode 二分查找 Search Insert Position
- LeetCode @ Search Insert Position 二分查找
- LeetCode 35. Search Insert Position 二分查找
- (二分查找)LeetCode#35. Search Insert Position
- [LeetCode]Search Insert Position (二分查找总结) Java Version
- Search Insert Position Leetcode 非二分查找法
- leetcode 35. Search Insert Position-二分查找|递归|非递归
- leetcode 35. Search Insert Position搜索插入位置(二分查找)
- LeetCode—Search Insert Position
- Search Insert Position:二分搜索
- leetcode---search-insert-position---查找
- Leetcode ☞ 35. Search Insert Position 【binary search 二分查找 模板】
- LeetCode——Search Insert Position
- LeetCode——Search Insert Position
- leetcode 035 —— Search Insert Position
- LeetCode35——Search Insert Position
- hdu 题目分类
- 单词读音
- 【codeforces】-610A-Pasha and Stick(水)
- 25个最受用户喜爱的免费WordPress插件:第1个~第5个
- 8086汇编语言 使用link连接,生成exe文件
- 二分查找——Search Insert Position
- notepad++ 左面显示文件夹结构
- 文章标题POJ 2785:4 Values whose Sum is 0?(二分)
- 最小公倍数
- Listview 与 Button和Text的焦点冲突问题~
- apk前缀debug、release、unaligned的区别
- Oracle中的不等于号
- javaweb学生管理系统的课程设计
- easyUI datagrid 取选中行id