Search Insert Position - Leetcode 35 Python

preview_player
Показать описание
🔴 Question Link -

✅Connect with me

🔴 Question with Example

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.

Example 1:

Input: [1,3,5,6], 5
Output: 2
Рекомендации по теме
Комментарии
Автор

if there is an value provide the nums.index(val) else append that value to nums and sort the nums and return the index..

AmithTalentPlace
Автор

x = [1, 3, 5, 6]
val = 0
try :
print(x.index(val))
except:
print(len([i for i in x if i < val]))

jbn
Автор

def ins():
num= [-1, 1, 3, 5, 6]
index=0
for i in range(len(num)):
if num[i]==index:
return i
elif num[i]<index<num[i+1]:
return i+1
elif index==0:
return 0
elif index>num[len(num)-1]:
return len(num)
elif index<num[0]:
return 0

girdharj
Автор

input = [1, 3, 5, 7, 10]

search_value = 8

for i in range(0, len(input)):
if input[i] == search_value:
print(i)
break
elif input[i] > search_value:
position = i
print(position)
break

milindabackup