[LeetCode] 34. Find First and Last Position of Element in Sorted Array
Python
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
answer = []
for i in range(len(nums)):
if nums[i] == target:
answer.append(i)
if len(answer) == 1:
answer.append(answer[0])
if len(answer) > 2:
return [answer[0], answer[-1]]
if answer:
return answer
else:
return [-1, -1]
댓글남기기