leetcode 34 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
跟定一个有序数组和一个目标,如果目标在数组里找到目标的下标,如果不在,找到目标应该插入的位置。
二分查找,找到则返回mid,否则返回r+1或则l。
class Solution {
public:
int searchInsert(int A[], int n, int target)
{
if (n == 0)
return 0;
int l = 0, r = n - 1, mid = 0;
while(l <= r)
{
mid = (l + r) / 2;
if (A[mid] == target)
return mid;
if (A[mid] < target)
l = mid + 1;
else
r = mid - 1;
}
return l; // 返回的是l,或者是r+1
}
};
2015/03/31:
从左到右找到相等的或者找到第一次比target大的数的位置即为插入下标位置,但这个是On的,python如下:
class Solution:
# @param A, a list of integers
# @param target, an integer to be inserted
# @return integer
def searchInsert(self, A, target):
for i in range(len(A)):
if A[i] == target or A[i] > target:
return i
return len(A)
还是用第一种 二分法快吧
leetcode 34 Search Insert Position的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 【题解】【数组】【查找】【Leetcode】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] 35. Search Insert Position 解决思路
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- Android布局解析,图文(转)
LinearLayout:相当于Java GUI中的FlowLayout(流式布局),就是说一个组件后边跟一个,挨着靠,一个组件把一行占满了,就靠到下一行. linearlayoutdemo.xml ...
- php小写金额转大写
public static function amountInWords($num) { if (!is_numeric($num) || empty($num)) ...
- Instll meld in windows
在linux下用meld感觉颇为不错,但是在windows上真是折腾老久.p4merge也试了试, 但还是meld用起来更顺手. 使用 https://wiki.gnome.org/Meld/Wind ...
- iOS UITableViewCell AccessoryType属性
写了这么长时间的ios table 竟然不知道有一个Accessory来显示详情设置小图标,曾经竟然傻帽的取用一张图片来显示,如今想想真是..那痛啊.... cell.accessoryType = ...
- DataNode连接错误Retrying connect to server
在Hadoop分布式模式部署完毕后,通过start-dfs.sh启动NameNode.DataNode.SecondaryNameNode,在master节点通过jps命令查看,看到NameNod ...
- Grunt 之 Connect
前端开发 Grunt 之 Connect 在前端开发过程中,我们需要在开发过程中,将开发中的站点部署到服务器上,然后,在浏览器中查看实际的效果,在 Grunt 环境下,可以直接使用集成在 Grunt ...
- Notification使用以及PendingIntent.getActivity() (转)
public void sendNotification(Context ctx,String message) { //get the notification manager String ns ...
- WinHEC(Windows硬件project产业创新峰会)将2015回归
WinHEC这是Windows Hardware Engineering Cumminity,中国呼吁Windows硬件project产业创新峰会.将2015在早期的回报,2015年3月18日至19日 ...
- 汉高澳大利亚sinox2014电影播放flash最好的办法是安装游戏windows文本firefox
事实上,韩澳sinox本身是没有原生flashplayer,无论怎么捣鼓,它们是从adobe弄linux要么windows版本号flashplayer,它不停地拨弄linux版本号flashplaye ...
- Scrapy研究和探索(五岁以下儿童)——爬行自己主动多页(抢别人博客所有文章)
首先.在教程(二)(http://blog.csdn.net/u012150179/article/details/32911511)中,研究的是爬取单个网页的方法.在教程(三)(http://blo ...