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 ...
随机推荐
- Tomcat8
一.Apache Tomcat 8介绍 Tomcat 8.0.0-RC3 (alpha) Released ...
- React 官网列子学习
一个有状态的组件 除了接受输入数据(通过 this.props ),组件还可以保持内部状态数据(通过this.state ).当一个组件的状态数据的变化,展现的标记将被重新调用render() 更新. ...
- window下安装scala搭载Intellij IDE
最近由于公司业务需求,要用到scala,编写还是windows下较好,linux下运行比较靠谱,废话少说,直接上步骤! 1.首先安装java环境 jdk下载地址:http://www.oracle.c ...
- linux 命令——45 free(转)
free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...
- 使用bouncycastle进行DESede/DESeee/AES128/AES192/AES256的加解密
前言 默认的jdk不支持DESeee的算法,本地化的JDK中配置有拦截规则,可以通过使用bouncycastle的jar包中的DESEngine类来进行DESeee算法的运算. DES的8字节加解密 ...
- IOS 自定义Layer(图层)
方式1: @interface NJViewController () @end @implementation NJViewController - (void)viewDidLoad { [sup ...
- IOS view拖拽(触摸事件)
• iOS中的事件可以分为3大类型 触摸事件 加速计事件 远程控制事件 响应者对象 • 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事 件.我们称之为“响应 ...
- IOS NSBundle使用(访问文件夹)
NSBundle的相关信息 1.一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2.利用mainBundle就可以访问软件资源包中的任何资源 3.模拟器应用程序的安装路径: ...
- 【51nod1743】雪之国度(最小生成树+倍增)
点此看题面 大致题意: 给你一张无向连通图,其中每条边的边权为这条边连接的两点的权值之差.每次询问两点之间是否存在两条不重复的路径,若存在则输出这两条路径上最大值的最小值. 大致思路 这题显然就是要让 ...
- 【51nod1677】treecnt(树上数学题)
点此看题面 大致题意: 给你一个节点从1~n编号的树,让你从中选择k个节点并通过选择的边联通,且要使选择的边数最少,让你计算对于所有选择k个节点的情况最小选择边数的总和. 题解 这道题乍一看很麻烦:最 ...