Search Insert Position
来源:互联网 发布:小票小标签打印软件 编辑:程序博客网 时间:2024/06/14 11:15
Given 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 Solution { public int binarySearch(int[] nums, int target) { int low = 0; int high = nums.length - 1; while(low <= high) { int mid = (low + high) / 2; if(nums[mid] == target) return mid; else if(nums[mid] > target) high = mid - 1; else low = mid + 1; } return low; } public int searchInsert(int[] nums, int target) { if(nums == null || nums.length == 0) throw new IllegalArgumentException(); return binarySearch(nums, target); }}
0 0
- LeetCode: Search Insert Position
- Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode34:Search Insert Position
- Search Insert Position
- [35]Search Insert Position
- Search Insert Position
- Search Insert Position
- [LeetCode]Search Insert Position
- Search Insert Position
- [LeetCode] Search Insert Position
- Search Insert Position
- Search Insert Position
- 期望概率dp lightoj 1038 Race to 1 Again
- 3D引擎:Horde3D:小感3
- Java课程设计笔记-在ToolBar上显示ActionMode内容
- 自定义UITableViewCell初始化代码
- Java的接口基本要点
- Search Insert Position
- centos7 安装nginx
- SAS 更新license时遇到问题的解决
- 实现滚动tab切换fragment + viewpager
- poj 2586 Y2K Accounting Bug
- R语言学习笔记
- 【黑马程序员】C语言文件
- javaScript location对象
- Java 中如果有接口方法,子类是否一定要含有接口的实现题呢