mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-08 13:24:07 +08:00
refactor: Follow the PEP 585 Typing standard (#439)
* Follow the PEP 585 Typing standard * Update list.py
This commit is contained in:
@@ -4,11 +4,7 @@ Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
def binary_search(nums: List[int], target: int) -> int:
|
||||
def binary_search(nums: list[int], target: int) -> int:
|
||||
""" 二分查找(双闭区间) """
|
||||
# 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
i, j = 0, len(nums) - 1
|
||||
@@ -23,7 +19,7 @@ def binary_search(nums: List[int], target: int) -> int:
|
||||
return -1 # 未找到目标元素,返回 -1
|
||||
|
||||
|
||||
def binary_search1(nums: List[int], target: int) -> int:
|
||||
def binary_search1(nums: list[int], target: int) -> int:
|
||||
""" 二分查找(左闭右开) """
|
||||
# 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
i, j = 0, len(nums)
|
||||
@@ -42,7 +38,7 @@ def binary_search1(nums: List[int], target: int) -> int:
|
||||
""" Driver Code """
|
||||
if __name__ == '__main__':
|
||||
target: int = 6
|
||||
nums: List[int] = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]
|
||||
nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]
|
||||
|
||||
# 二分查找(双闭区间)
|
||||
index: int = binary_search(nums, target)
|
||||
|
||||
Reference in New Issue
Block a user