lintcode python 容易 60.搜索插入位置
2017-08-28 16:13
375 查看
60.搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。你可以假设在数组中无重复元素。
样例:
[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6],0 → 0
代码:
class Solution:
"""
@param: A: an integer sorted array
@param: target: an integer to be inserted
@return: An integer
"""
def searchInsert(self, A, target):
# write your code here
if A is None: return None
if A == []: return 0
n = len(A)
if A[0] >= target:
return 0
if A[n - 1] < target:
return n
for i in range(n):
while(A[i] == target):
return i
for i in range(n - 1):
if target > A[i] and target < A[i + 1]:
return i + 1
相关文章推荐
- lintcode 容易题:Search Insert Position 搜索插入位置
- LintCode 60 搜索插入位置
- LintCode 60. 搜索插入位置
- LintCode : 搜索插入位置
- lintcode&九章算法——No.60 搜索插入位置 ? 待解决
- LintCode_060_搜索插入位置
- LintCode_060_搜索插入位置
- Lintcode搜索插入位置
- python_lintcode_64合并排序数组 II_60搜索插入位置_142 O(1)时间检测2的幂次
- lintcode:搜索插入位置
- lintcode 173 单链表插入排序 python
- Lintcode 容易 python 判断字符串是否没有重复字符
- 60 - 搜索插入位置
- 60.搜索插入位置
- lintcode 容易题:Insert Interval 插入区间
- lintcode-60-搜索插入位置
- Leet Code 35 Search Insert Position - 搜索插入位置-Java
- lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点
- Lintcode:搜索二维矩阵
- 趣学python第3章字符串2把值插入到某位置