This commit is contained in:
krahets
2023-03-03 02:46:12 +08:00
parent 122805bdc9
commit cf9d102ed5
24 changed files with 320 additions and 314 deletions
+6 -6
View File
@@ -98,8 +98,8 @@ $$
=== "Python"
```python title="binary_search.py"
""" 二分查找(双闭区间) """
def binary_search(nums, target):
""" 二分查找(双闭区间) """
# 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
i, j = 0, len(nums) - 1
while i <= j:
@@ -145,7 +145,7 @@ $$
let i = 0, j = nums.length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j) {
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
@@ -167,7 +167,7 @@ $$
let i = 0, j = nums.length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j) {
const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m
const m = Math.floor((i + j) / 2); // 计算中点索引 m
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m-1] 中
@@ -309,8 +309,8 @@ $$
=== "Python"
```python title="binary_search.py"
""" 二分查找(左闭右开) """
def binary_search1(nums, target):
""" 二分查找(左闭右开) """
# 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
i, j = 0, len(nums)
# 循环,当搜索区间为空时跳出(当 i = j 时为空)
@@ -357,7 +357,7 @@ $$
let i = 0, j = nums.length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
@@ -379,7 +379,7 @@ $$
let i = 0, j = nums.length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m
const m = Math.floor((i + j) / 2); // 计算中点索引 m
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m) 中
+4 -4
View File
@@ -45,8 +45,8 @@ comments: true
=== "Python"
```python title="hashing_search.py"
""" 哈希查找(数组) """
def hashing_search_array(mapp, target):
""" 哈希查找(数组) """
# 哈希表的 key: 目标元素,value: 索引
# 若哈希表中无此 key ,返回 -1
return mapp.get(target, -1)
@@ -85,7 +85,7 @@ comments: true
function hashingSearchArray(map: Map<number, number>, target: number): number {
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.has(target) ? map.get(target) as number : -1;
return map.has(target) ? (map.get(target) as number) : -1;
}
```
@@ -163,8 +163,8 @@ comments: true
=== "Python"
```python title="hashing_search.py"
""" 哈希查找(链表) """
def hashing_search_linkedlist(mapp, target):
""" 哈希查找(链表) """
# 哈希表的 key: 目标元素,value: 结点对象
# 若哈希表中无此 key ,返回 -1
return mapp.get(target, -1)
@@ -203,7 +203,7 @@ comments: true
function hashingSearchLinkedList(map: Map<number, ListNode>, target: number): ListNode | null {
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.has(target) ? map.get(target) as ListNode : null;
return map.has(target) ? (map.get(target) as ListNode) : null;
}
```
+2 -2
View File
@@ -49,8 +49,8 @@ comments: true
=== "Python"
```python title="linear_search.py"
""" 线性查找(数组) """
def linear_search_array(nums, target):
""" 线性查找(数组) """
# 遍历数组
for i in range(len(nums)):
if nums[i] == target: # 找到目标元素,返回其索引
@@ -206,8 +206,8 @@ comments: true
=== "Python"
```python title="linear_search.py"
""" 线性查找(链表) """
def linear_search_linkedlist(head, target):
""" 线性查找(链表) """
# 遍历链表
while head:
if head.val == target: # 找到目标结点,返回之