给你一个有序的数字列表和一个目标数值,任务是在列表中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
备注:列表是无重复的,均为正整数。
题目难度:简单
题目来源:leetcode:搜索插入位置
def search_insert(nums:list, target):
pass
assert search_insert([1,3,5,6], 5) == 2
assert search_insert([1,3,5,6], 2) == 1
assert search_insert([1,3,5,6], 7) == 4
assert search_insert([1,3,5,6], 0) == 0
fwj
(fwj)
2021 年7 月 14 日 01:37
2
求简单解法。。
def serach_insert(li_a,n):
for i in range(len(li_a)):
if li_a[i]==n:
return i
else:
li_a.append(n)
li_a.sort()
for j in range(len(li_a)):
if li_a[j]==n:
return j
assert serach_insert([1,3,5,6], 5) == 2
assert serach_insert([1,3,5,6], 2) == 1
assert serach_insert([1,3,5,6], 7) == 4
assert serach_insert([1,3,5,6], 0) == 0
def search_insert(nums: list, target):
i = 0
while i <= len(nums) - 1 and nums[i] < target:
i += 1
return i
assert search_insert([1, 3, 5, 6], 5) == 2
assert search_insert([1, 3, 5, 6], 2) == 1
assert search_insert([1, 3, 5, 6], 7) == 4
assert search_insert([1, 3, 5, 6], 0) == 0
def searchInsert(nums, target):
nums.append(target)
nums.sort()
return nums.index(target)
“”"
def search_insert(nums: list, target):
# 优化
print(target)
# 将目标数据插入到列表后面
nums.append(target)
# 将列表排序
nums.sort()
print(nums)
# 返回目标数据索引值(位置)
return nums.index(target)
class TestSearchInsert:
def test_search(self):
assert search_insert([1, 3, 5, 6], 5) == 2
assert search_insert([1, 3, 5, 6], 2) == 1
assert search_insert([1, 3, 5, 6], 0) == 0
assert search_insert([1, 3, 5, 6], 7) == 4
# or 用循环判断
# for i in range(len(nums)):
#
# if nums[i] == target:
# print(nums)
# return i
# elif nums[i] > target:
# nums.insert(i, target)
# print(nums)
# return i
# elif nums[len(nums)-1]<target:
# nums.append(target)
# print(nums)
# return nums.index(target)
“”"
def search_insert(nums, target):
""" 搜索插入位置 """
if target in nums:
return nums.index(target)
else:
nums.append(target)
nums.sort(reverse=False)
return nums.index(target)
@cold_watermelon 666666
@fwj 参看这位大佬的优秀思路
cold_watermelon:
def search_insert(nums: list, target):
i = 0
while i <= len(nums) - 1 and nums[i] < target:
i += 1
return i
assert search_insert([1, 3, 5, 6], 5) == 2
assert search_insert([1, 3, 5, 6], 2) == 1
assert search_insert([1, 3, 5, 6], 7) == 4
assert search_insert([1, 3, 5, 6], 0) == 0
@89717045_7695 , @13718723532 同学你好,题解都很棒!同时,建议尽量使用markdown文本哈,这样方便其他同学拷贝学习你的代码。具体使用方式是,在回复中,贴源码,然后前、后使用两个``` 符号包围。例如:
my codes here
1 个赞
class Solution:
def search_insert(self: list, target) -> int:
"""
1、如果目标值在列表中存在,则返回其索引
2、如果目标值在列表中不存在,返回它将按顺序插入的位置
:param self:
:param target:
:retursn: int:
"""
if target in self:
return [i for i, j in list(enumerate(self)) if j == target][0]
else:
self.append(target)
self.sort()
return self.index(target)
assert search_insert([1, 3, 5, 6], 5) == 2
assert search_insert([1, 3, 5, 6], 2) == 1
assert search_insert([1, 3, 5, 6], 7) == 4
assert search_insert([1, 3, 5, 6], 0) == 0
普通青年
def search_insert(nums, target) -> int:
if target in nums:
return nums.index(target)
for i,item in enumerate(nums):
if target < item:
return i
if item < target:
if i < len(nums) -1:
continue
else:
return i+1
assert search_insert([1, 3, 5, 6], 5) == 2
assert search_insert([1, 3, 5, 6], 2) == 1
assert search_insert([1, 3, 5, 6], 7) == 4
assert search_insert([1, 3, 5, 6], 0) == 0
力扣官方题解python版:二分查找
class Solution:
def search_insert(self, nums, target: int) -> int:
n = len(nums)
left, right, ans = 0, n-1, n
while left <= right:
mid = ((right - left) >> 1) + left
if target <= nums[mid]:
ans, right = mid, mid - 1
else:
left = mid + 1
return ans
复杂度分析
时间复杂度:O(logn),其中 n 为数组的长度。二分查找所需的时间复杂度为 O(logn)。
空间复杂度:O(1)。我们只需要常数空间存放若干变量。
参见链接:力扣
def search_insert(nums: list, target):
# 优化
print(target)
# 将目标数据插入到列表后面
nums.append(target)
# 将列表排序
nums.sort()
print(nums)
# 返回目标数据索引值(位置)
return nums.index(target)
class TestSearchInsert:
def test_search(self):
assert search_insert([1, 3, 5, 6], 5) == 2
assert search_insert([1, 3, 5, 6], 2) == 1
assert search_insert([1, 3, 5, 6], 0) == 0
assert search_insert([1, 3, 5, 6], 7) == 4
# or 用循环判断
# for i in range(len(nums)):
#
# if nums[i] == target:
# print(nums)
# return i
# elif nums[i] > target:
# nums.insert(i, target)
# print(nums)
# return i
# elif nums[len(nums)-1]<target:
# nums.append(target)
# print(nums)
# return nums.index(target)
‘’’
def search_insert(list, target):
left = 0
right = len(list)-1
flag = 0
if (target > list[right]):
return right+1
while(left < right):
i = int(left + (right - left)/2)
if (target > list[i]):
left = i + 1
else:
right = i
flag = left
return flag
‘’’