mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 06:10:19 +08:00
Update code style for Python
This commit is contained in:
@@ -6,10 +6,10 @@ Author: timi (xisunyy@163.com)
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
from modules import *
|
||||
|
||||
""" 二分查找(双闭区间) """
|
||||
def binary_search(nums, target):
|
||||
""" 二分查找(双闭区间) """
|
||||
# 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
i, j = 0, len(nums) - 1
|
||||
while i <= j:
|
||||
@@ -23,8 +23,8 @@ def binary_search(nums, target):
|
||||
return -1 # 未找到目标元素,返回 -1
|
||||
|
||||
|
||||
""" 二分查找(左闭右开) """
|
||||
def binary_search1(nums, target):
|
||||
""" 二分查找(左闭右开) """
|
||||
# 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
i, j = 0, len(nums)
|
||||
# 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
|
||||
@@ -6,16 +6,16 @@ Author: timi (xisunyy@163.com)
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
from modules import *
|
||||
|
||||
""" 哈希查找(数组) """
|
||||
def hashing_search_array(mapp, target):
|
||||
""" 哈希查找(数组) """
|
||||
# 哈希表的 key: 目标元素,value: 索引
|
||||
# 若哈希表中无此 key ,返回 -1
|
||||
return mapp.get(target, -1)
|
||||
|
||||
""" 哈希查找(链表) """
|
||||
def hashing_search_linkedlist(mapp, target):
|
||||
""" 哈希查找(链表) """
|
||||
# 哈希表的 key: 目标元素,value: 结点对象
|
||||
# 若哈希表中无此 key ,返回 -1
|
||||
return mapp.get(target, -1)
|
||||
|
||||
@@ -6,18 +6,18 @@ Author: timi (xisunyy@163.com)
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
from modules import *
|
||||
|
||||
""" 线性查找(数组) """
|
||||
def linear_search_array(nums, target):
|
||||
""" 线性查找(数组) """
|
||||
# 遍历数组
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target: # 找到目标元素,返回其索引
|
||||
return i
|
||||
return -1 # 未找到目标元素,返回 -1
|
||||
|
||||
""" 线性查找(链表) """
|
||||
def linear_search_linkedlist(head, target):
|
||||
""" 线性查找(链表) """
|
||||
# 遍历链表
|
||||
while head:
|
||||
if head.val == target: # 找到目标结点,返回之
|
||||
|
||||
Reference in New Issue
Block a user