题目

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

代码:oj测试通过 Runtime: 52 ms

 class Solution:
# @param A, a list of integers
# @param target, an integer to be inserted
# @return integer
def search(self, A, start, end, target):
# search stop case I
if start == end:
if start == 0:
return [ 0,1 ][ A[0]<target ]
if A[start] == target:
return start
else:
return [start,start+1][A[start]<target]
# search stop case II
if start+1 == end:
if A[start] >= target:
return start
elif A[start] < target and A[end] >= target :
return end
else:
return end+1 mid = (start+end)/2
# search stop case III
if A[mid] == target:
return mid
if A[mid] > target:
return self.search(A, start, mid-1, target)
if A[mid] < target:
return self.search(A, mid+1, end, target) def searchInsert(self, A, target):
# zero length case
if len(A) == 0:
return 0
# binary search
start = 0
end = len(A)-1
return self.search(A, start, end, target)

思路

二分查找经典题。

采用迭代方式时:

1. 考虑start==end的情况(一个元素)和start+1==end的情况(两个元素),作为迭代终止的两种case。

2. 当元素数量大于3时作为一般的case处理,二分查找。

3. 根据题意要求进行判断条件。

4. 第一次提交没有AC ,原因是在处理start==end的case时候,竟然只考虑了0和len(A)的边界情况,没有考虑一般情况,陷入了思维的陷阱。

后面又写了一版非递归的代码:oj测试通过 Runtime: 63 ms

 class Solution:
# @param A, a list of integers
# @param target, an integer to be inserted
# @return integer
def searchInsert(self, A, target):
# zero length case
if len(A) == 0 :
return 0
# binary search
start = 0
end = len(A)-1
while start <= end :
if start == end:
if start == 0:
return [0,1][A[0]<target]
if A[start] == target:
return start
else:
return [start,start+1][A[start]<target]
if start+1 == end:
if A[start] >= target:
return start
elif A[start] < target and A[end] >= target:
return end
else:
return end+1
mid = (start+end)/2
if A[mid] == target:
return mid
elif A[mid] > target:
end = mid - 1
else:
start = mid + 1

思路跟非递归差不太多。

个人感觉判断stop case的代码虽然逻辑上比较清晰(剩一个元素或者两个元素或者直接找到了target),但是并不是很简洁。后续再不断改进。

leetcode 【 Search Insert Position 】python 实现的更多相关文章

  1. leetcode Search Insert Position Python

    #Given a sorted array and a target value, return the index if the target is found. If #not, return t ...

  2. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  4. 35. Search Insert Position@python

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. [LeetCode] Search Insert Position 二分搜索

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  9. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  10. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. (已解决)Arduino mega2560 R3插在电脑上没有反应

           OK,话不多说.网上找了一些资料,感觉都说的不够清晰.自己琢磨了下,有了一个简单粗暴的方法. 步骤1:插上Arduino mega2560板子.没有反应. 步骤2:我的电脑-管理-设备管 ...

  2. [转]vim 快捷键整理

    Linux中vim编辑器的功能非常强大,许多常用快捷键用起来非常方便,这里将我学vim入门时学的一些常用的快捷键分享给大家一下,希望可以帮助你们.  原文地址:http://blog.csdn.net ...

  3. WPF:鼠标长时间无操作,窗口隐藏

    //设置鼠标长时间无操作计时器 private System.Timers.Timer MouseTimerTick = new System.Timers.Timer(10000); private ...

  4. silverlight数据绑定模式TwoWay,OneWay,OneTime的研究

    asp.net开发中,数据绑定是一个很简单的概念,控件与数据绑定后,控件可以自动把数据按一定的形式显示出来.(当然控件上的值改变后,可以通过提交页面表单,同时后台服务端代码接收新值更新数据) silv ...

  5. 飞塔Web应用防火墙-FortiWeb

    飞塔Web应用防火墙-FortiWeb 平台: fortiweb 类型: 虚拟机镜像 软件包: linux basic software Fortinet security SSL offloadin ...

  6. 绿卡基础知识:I-129

    绿卡基础知识:I-129 标签: 绿卡基础知识 I-129 表格本不该你来填的.那是你老板的 business.在美国工作,除非是公民或有绿卡,都需要移民局的批准.如果你没有 EAD,I-129 就是 ...

  7. ubuntu openstack windows 镜像制作

    openstack  windows 镜像制作 首先下载windows所需要的驱动,virtio-win-1.1.16.vfd virtio-win-0.1-59.iso 下载的官方地址是: http ...

  8. SAP Netweaver的负载均衡消息服务器 vs CloudFoundry的App Router

    Message server for ABAP Netweaver SAP传统应用经典的三层架构: 起到负载均衡的消息服务器(Message Server)在图中没有得到体现.然后,消息服务器在我们每 ...

  9. MYSQL短索引

    优化MYSQL时,可以尽量使用短索引,如果只是为了提高读取的速度,可以优先使用聚合索引,把几个字段聚集在一起,当然缺点在于操作(写)的时候会降低效率,短索引一般都是开头几个字符基本不同的时候,可以考虑 ...

  10. iOS keychain注解

    + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictiona ...