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 ...
随机推荐
- redis在Windows下以后台服务一键搭建集群(多机器)
redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...
- HDU3973 线段树 + 字符哈希
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
- mongodb 入坑
一.安装mongodb https://www.mongodb.com/ 官网下载合适的版本,安装在C或者D盘,我选择的是默认路径C:\Program Files\MongoDB\Server\3.4 ...
- Windows Phone Emulator 模拟器常用快捷键
在使用Windows Phone 的开发的时候,在目前大家还很难买到真实的Windows Phone 设备的情况下,我们用来调试自己的程序经常用到的可能就是Emulator了.经常会有人问我说,用鼠标 ...
- NSAttributedString能否设置文字下划线?是否支持line break?
#import <CoreText/CoreText.h> #import "ViewController.h" @interface ViewController ( ...
- appuim操作webview控件
1.操作webview控件,在uiautomator中如下图,能定位的只有最外层的内容.就一个webview控件,查找不到里面内容 1.使用driver.getContext(),获取是否是webvi ...
- 【洛谷1967】货车运输(最大生成树+倍增LCA)
点此看题面 大致题意: 有\(n\)个城市和\(m\)条道路,每条道路有一个限重.多组询问,每次询问从\(x\)到\(y\)的最大载重为多少. 一个贪心的想法 首先,让我们来贪心一波. 由于要求最大载 ...
- Nginx+proxy实现简单的负载均衡
环境说明:操作系统centos6.6 64位web操纵系统是:web1=192.168.10.10(LAMP) web2=192.168.10.11(LNMP),这里只是测试nginx实现负载均衡效果 ...
- 将数据库数据添加到ListView控件中
实现效果: 知识运用: ListView控件中的Items集合的Clear方法 //从listView控件的数据项集合中移除所有数据项 补充:可以使用Remove或RemoveAt方法从集合中移除单个 ...
- 2018.5.24 Oracle下的sqlplus编程 块结构
1.语句结构模板 declare --声明 begin dbms_output.put_line('Legend Hello world'); end; 2.变量使用 & 是输入符号 decl ...