View
#Array #Binary_Search
URL
Intuition
- start, middle, end 포인터를 사용하여 Binary Search를 진행한다.
- str으로 바꿔서 find함수를 써보려 했는데 마이너스 기호 때문에 불가능하다.
Solution
class Solution:
def search(self, nums: List[int], target: int) -> int:
start, end = 0, len(nums)
while start < end:
middle = (start+end)//2
if nums[middle] == target:
return middle
elif nums[middle] < target:
start = middle + 1
else:
end = middle
return -1
.
'CS > Coding Test' 카테고리의 다른 글
[LeetCode] 733. Flood Fill (0) | 2023.07.20 |
---|---|
[LeetCode] 242. Valid Anagram (0) | 2023.07.20 |
[LeetCode] 226. Invert Binary Tree (0) | 2023.07.20 |
[LeetCode] 125. Valid Palindrome, 4가지 solution 성능 비교 (0) | 2023.07.20 |
[LeetCode] 121.Best Time to Buy and Sell Stock, Time Limit Exceeded (0) | 2023.07.17 |
reply