소프트웨어에 대한 모든 것

LeetCode 풀기 - 704. Binary Search 본문

알고리즘/LeetCode

LeetCode 풀기 - 704. Binary Search

앤테바 2021. 11. 10. 06:16
반응형

704. Binary Search

https://leetcode.com/problems/binary-search/

 

Binary Search - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

이진 탐색 (binary search)

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)
        while left < right:
            mid = (left + right) // 2
            
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid
        
        return -1

 

반응형
Comments