[Python] 파이썬/Leetcode_14Days plan to crack Algo

[Leetcode] 704. Binary Search {Day1 Binary Search} (In Python)

두부군_ 2021. 11. 25. 12:08

간략한 코드들은 github.com/Eastar-DS/Python 의 Leetcode 폴더에도 있습니다

 

    Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
   주어진 리스트 nums는 숫자들이 오름차순으로 정렬되어있고 타겟은 정수입니다. 타겟이 nums안에 있으면 몇번째 인덱스에 있는지 반환하고 아니면 -1을 반환하는 함수를 만들어봅시다.



    Example 1:

    Input: nums = [-1,0,3,5,9,12], target = 9
    Output: 4
    Explanation: 9 exists in nums and its index is 4
    Example 2:

    Input: nums = [-1,0,3,5,9,12], target = 2
    Output: -1
    Explanation: 2 does not exist in nums so return -1