Description
Given a sorted array of distinct integers 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 must write an algorithm with O(log n) runtime complexity.
My solution
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
if target in nums:
return nums.index(target)
else:
nums.append(target)
return sorted(nums).index(target)
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
if target in nums:
return nums.index(target)
else:
for i, val in enumerate(nums):
if target < val:
return i
elif target > max(nums):
return len(nums)
'Algorithm > Daily Coding Tests Challenge' 카테고리의 다른 글
| [프로그래머스] level2. 소수 찾기 (0) | 2021.09.07 |
|---|---|
| [프로그래머스] level2. 구명보트 (0) | 2021.09.06 |
| [프로그래머스] level2. JadenCase 문자열 만들기 (0) | 2021.09.05 |
| [Leetcode] Easy : Implement strStr() (0) | 2021.09.04 |
| [프로그래머스] level2. 카펫 (0) | 2021.09.04 |