Search Insert Position - Leetcode
来源:互联网 发布:nginx web服务器配置 编辑:程序博客网 时间:2024/05/28 20:19
public class Solution { public int searchInsert(int[] A, int target) { int start = 0, end = A.length-1;int mid; while(start<=end){ mid = (start+end)/2; if(A[mid] == target) return mid; else if(target > A[mid]) start = mid+1; else end = mid-1; } return start; }}分析:返回的是插入的值,start的位置。
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
0 0
- LeetCode: 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
- [LeetCode] 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
- Leetcode: Search Insert Position
- LeetCode:Search Insert Position
- leetcode Search Insert Position
- LeetCode Search Insert Position
- HDU 1698 Just a Hook
- 动态规划 求部分和
- 使MSHFlexGrid支持鼠标滚动
- 设计模式介绍:命令模式(command)
- POJ 3468 A Simple Problem with Integers
- Search Insert Position - Leetcode
- 设计模式介绍:模板方法(Template Method)模式
- ios 不使用控件设计app
- super关键字的使用
- 是时候开始学习 Ionic了
- 大家好。新来的。
- HDU 1342 Lotto 【DFS】
- Linux IPC实践(12) --System V信号量(2)
- Search for a Range - Leetcode