LeetCode OJ算法题(三十四):Search Insert Position
来源:互联网 发布:文华财经交易模型源码 编辑:程序博客网 时间:2024/05/22 10:34
题目:
iven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[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 No34_SearchInsertPosition {public static void main(String[] args){System.out.println(searchInsert(new int[]{1,2,3,4,5,6,8}, 7));}public static int searchInsert(int[] A, int target) { int low = 0; int high = A.length-1; while(low <= high){ int mid = (high-low)/2 +low; if(target == A[mid]) return mid; if(target < A[mid]){ high = mid-1; continue; } if(target > A[mid]){ low = mid+1; continue; } } return low; }}
0 0
- LeetCode OJ算法题(三十四):Search Insert Position
- LeetCode OJ:Search Insert Position
- LeetCode OJ - Search Insert Position
- [LeetCode OJ]Search Insert Position
- LeetCode OJ Search Insert Position
- LeetCode OJ - Search Insert Position
- LeetCode OJ 之 Search Insert Position (查找插入位置)
- 【LeetCode OJ 035】Search Insert Position
- <LeetCode OJ> 35. Search Insert Position
- LeetCode OJ-35-Search Insert Position
- python写算法题:leetcode: 35. Search Insert Position
- 【LeetCode算法练习(C++)】Search Insert Position
- LeetCode: Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- 静态库.a 文件是如何生成的
- 解决Undefined variable: _SESSION的方法
- weblogic下ajax无法进入返回函数
- 几个有用的Shell,解析json,解析URL
- zookeeper安装配置
- LeetCode OJ算法题(三十四):Search Insert Position
- cmd 模块构建,从认识grunt开始
- C3P0在Linux下的Failed to get local InetAddress for VMID解决方法
- java动态代理(JDK和cglib)
- Qt在Windows下的三种编程环境搭建
- 娓娓道来c指针 (5)c数组本质
- Cause: java.io.UTFDataFormatException: Invalid byte 3 of 3-byte UTF-8 sequence. 解决办法
- Activiti工作流简介
- NC(NetCat)命令详解