题目

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. C#类型简述

    一.值类型 1.布尔类型 bool,范围 true false 2.整数类型 sbyte,范围 -128~127 byte,范围 0~255 short,范围 -32768~32767 ushort, ...

  2. Yii2 widgets [mztest/yii2-widget-file-upload]

    Enjoy it. A widget for uploading files to your server. Github , Packagist Screenshots

  3. Flexbox与Grid属性比较

    网格容器(container)属性 网格项目(item)属性 Flex容器(container)属性 Flex项目(item)属性

  4. 查询MySQL的存储引擎

  5. 新建snmp模型总结

    1.在DeviceType.xml中添加新的模块 2.在modellidx.json中添加路径关联 3.添加定义模型 4.定义model.xml注意: 5.定义collect.xml注意:

  6. 2018. 2.4 Java中集合嵌套集合的练习

    创建学生类有姓名学校和年龄 覆盖toString() 1.创建三个学生对象,放到集合ArrayList 2.输出第2名学生的信息 3.删除第1个学生对象 4.在第2个位置插入1个新学生信息 5.判断刘 ...

  7. eclipse中关闭java文件后再打开,找不到如何切换可视化编辑器

    http://www.iteye.com/problems/64806 两种方式:   第一种正如楼上的老兄说的.在该java类中,鼠标右击,移动到openWith上 找到jigloo,找不到就在ot ...

  8. .NET AJAX实例

    引用地址:http://blog.csdn.net/qianjiu/article/details/7524228 5.2 Ajax基础http://book.csdn.net/bookfiles/6 ...

  9. [vijos1066]弱弱的战壕

    描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒 ...

  10. ElasticSearch部署问题

    以下几个是以前在自己部署ElaticSearch的时候收集到的,认为有用的 https://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ h ...