leetcode 【 Search Insert Position 】python 实现
题目:
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 实现的更多相关文章
- 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 ...
 - 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 ...
 - LeetCode: Search Insert Position 解题报告
		
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
 - 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 ...
 - [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] Search Insert Position 二分搜索
		
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
 - [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 Search Insert Position (二分查找)
		
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
 - LeetCode——Search Insert Position
		
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
 - [Leetcode] search insert position 寻找插入位置
		
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
 
随机推荐
- Unicode字符集
			
Unicode字符集的出现是为了弥补ASCII码只能表示128个字符的限制.在实际应用中,如若我们想显示汉字或日文等等,显然使用ASCII是不可能的.Unicode占用了两个字节,即16位,能表示的字 ...
 - C++ int转string / string转int
			
c++ 最近标准添加了stringstream类,可以非常简单的对int 和 string 进行相互的转化 //int 转 string void int2str(const int& i, ...
 - uvm_hdl——DPI在UVM中的实现(四)
			
我们可以在uvm中实现HDL的后门访问,具体包括的function有uvm_hdl_check_path,uvm_hdl_deposit, uvm_hdl_force,uvm_hdl_release, ...
 - Openfire+spark在linux上搭建内部聊天系统
			
一. 实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...
 - 获取win10 Insider Preview报错0x80080300
			
获取win10 Insider Preview报错0x80080300 1.msconfig2.隐藏Microsoft 服务3.disable 剩下的服务4.win + i, Update&s ...
 - js 中的 Math.ceil() Math.floor Math.round()
			
alert(Math.ceil(25.9)); alert(Math.ceil(25.5)); alert(Math.ceil(25.1)); alert(Math.round(25.9)); ale ...
 - <已解决> Eclipse启动失败
			
参考:http://stackoverflow.com/questions/15404964/starting-eclipse-results-in-failed-to-create-java-vir ...
 - IOS UIApplication使用
			
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
 - POI 怎么设置Excel整列的CellStyle啊
			
POI 怎么设置Excel整列的CellStyle啊,而不是循环每个Cell.因为现在是生成Excel模板,不知道客户会输入多少行. 问题补充: 指尖言 写道 好像没有这个方法,CellStyle是C ...
 - 实现带复选框的TreeView控件
			
实现效果: 知识运用: TreeView控件的CheckView属性 //是否在树形视图控件中显示复选框 public bool CheckBoxs{ get;ser } 实现代码: TreeView ...