35. Search Insert Position
2016-07-24 13:27
369 查看
35. Search Insert Position
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
Hide Tags
Array Binary Search
Hide Similar Problems
(E) First Bad Version
public class Solution { public int searchInsert(int[] nums, int target) { int left = 0; int right = nums.length-1; //find the first number that is >= than target if(nums[right] < target) return right+1; while(left<right){ int mid = (left+right)/2; if(nums[mid] == target) return mid; if(nums[mid] > target) right = mid; else left = mid + 1; } return left; } }
相关文章推荐
- 浏览器HTTP请求分析
- zookeeper如何永久监听
- Java通过反射机制动态设置对象属性值的方法
- 单片机关键字sfr和sbit的理解
- 常用shell命令操纵技巧
- Fraction to Recurring Decimal
- Tomcat 系统架构与设计模式1
- 排列,含重复元素和不重复元素两种情况的实现
- 常用批处理命令总结 <一>
- leetcode---Decode Ways---动规
- 虚拟机与主机共享文件夹步骤
- 高效的显示bitmap
- 一对一映射
- Android系统源码阅读(1):编译
- POJ Problem 3040 Allowance 【贪心】
- HDU1045 Fire Net(DFS枚举||二分图匹配)
- Java 项目的命名规范
- 现代操作系统--I/O设备(读书笔记)
- weblogic down 掉无法启动 ( 被锁)
- LeetCode:238. Product of Array Except Self(除自身外的乘积)