[Leetcode] Search Insert Position
来源:互联网 发布:我的心时刻牵挂着网络 编辑:程序博客网 时间:2024/05/01 08:48
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
class Solution {public: int searchInsert(int A[], int n, int target) { return binaryInsert(A,0,n-1,target); } int binaryInsert(int A[],int low,int high,int target) { if(low == high) { if (A[low] >= target) return low; else return low+1; } int mid = (low + high) / 2; if (A[mid] == target) return mid; else if (A[mid] > target) return binaryInsert(A,low,mid,target); else return binaryInsert(A,mid+1,high,target); }};
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
- samba实现ubuntu与windows文件共享
- 关于浮点数不能进行位运算的问题
- 对象构建会做什么
- 第十二周 求数的阶乘(递归)
- Linux常用命令(十四) - tail
- [Leetcode] Search Insert Position
- 上机作业 抽取一个数猜大小
- 关于php的一些小结
- [leetcode] Search for a Range
- XMLHttpRequest()的post和get方法
- Python学习笔记-第二章 列表和元组
- 2014.11.9--南京GDG活动摘要
- 关于两个Fragment布局报错Error inflating class fragment的解决方法
- spring 自定义注解的注册与扫描