给定排序数组和目标值,返回索引

时间:2018-01-21 21:31:37

标签: python

class Solution(object):
    def searchInsert(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: int
    """

        print target
        print nums
        for i in nums:
            print i
            if i==target:
                return nums.index(i)

            else:
                 if i>=target:
                    nums.append('target')-----[1](How to append the target here 
                                                   before the 'i' element to which 
                                                   compared)

                    else:
                        nums.append('target')

                print False

给定排序数组和目标值,返回索引。如果没有,返回索引按顺序插入的位置?

我的问题是: - 如果目标不同,那么如何在目标列表中的第i个元素之前附加目标? (我在问题中[1]明确提到过)

1 个答案:

答案 0 :(得分:0)

使用bisect模块中的一个功能。他们处理命中和未命中。

相关问题