- ์ด์ง ๊ฒ์ ์๊ณ ๋ฆฌ์ฆ ์ด์ฉ
target
๋ณด๋ค ์ค๊ฐ ๊ฐ์ด ์์ผ๋ฉด ์ฆ๊ฐ์์ผ๊ฐ์ผ ํ๊ณ
- ํฌ๋ฉด ๊ฐ์์์ผ๊ฐ์ผ ํ๊ณ
- ๊ฐ์ผ๋ฉด ๊ทธ
mid
๊ฐ์ด index
๊ฐ์ด๋ ๋ฆฌํดํ๋ฉด ๋จ
Solution
from typing import List
class Solution:
# O(log n)์ผ๋ก ํ๊ธฐ
def search(self, nums: List[int], target: int) -> int:
lt, rt = 0, len(nums) - 1
while lt <= rt:
mid = (lt + rt) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
mid += lt
lt += 1
continue
if nums[mid] > target:
mid -= rt
rt -= 1
return -1
Reference
Leave a comment