This commit is contained in:
krahets
2024-04-12 03:16:45 +08:00
parent 8b8168bb31
commit b5f94abec9
10 changed files with 217 additions and 101 deletions
+42 -2
View File
@@ -339,7 +339,27 @@ comments: true
=== "Ruby"
```ruby title="binary_search.rb"
[class]{}-[func]{binary_search}
### 二分查找(双闭区间) ###
def binary_search(nums, target)
# 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
i, j = 0, nums.length - 1
# 循环,当搜索区间为空时跳出(当 i > j 时为空)
while i <= j
# 理论上 Ruby 的数字可以无限大(取决于内存大小),无须考虑大数越界问题
m = (i + j) / 2 # 计算中点索引 m
if nums[m] < target
i = m + 1 # 此情况说明 target 在区间 [m+1, j] 中
elsif nums[m] > target
j = m - 1 # 此情况说明 target 在区间 [i, m-1] 中
else
return m # 找到目标元素,返回其索引
end
end
-1 # 未找到目标元素,返回 -1
end
```
=== "Zig"
@@ -667,7 +687,27 @@ comments: true
=== "Ruby"
```ruby title="binary_search.rb"
[class]{}-[func]{binary_search_lcro}
### 二分查找(左闭右开区间) ###
def binary_search_lcro(nums, target)
# 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
i, j = 0, nums.length
# 循环,当搜索区间为空时跳出(当 i = j 时为空)
while i < j
# 计算中点索引 m
m = (i + j) / 2
if nums[m] < target
i = m + 1 # 此情况说明 target 在区间 [m+1, j) 中
elsif nums[m] > target
j = m - 1 # 此情况说明 target 在区间 [i, m) 中
else
return m # 找到目标元素,返回其索引
end
end
-1 # 未找到目标元素,返回 -1
end
```
=== "Zig"
+23 -2
View File
@@ -212,7 +212,16 @@ comments: true
=== "Ruby"
```ruby title="binary_search_edge.rb"
[class]{}-[func]{binary_search_left_edge}
### 二分查找最左一个 target ###
def binary_search_left_edge(nums, target)
# 等价于查找 target 的插入点
i = binary_search_insertion(nums, target)
# 未找到 target ,返回 -1
return -1 if i == nums.length || nums[i] != target
i # 找到 target ,返回索引 i
end
```
=== "Zig"
@@ -461,7 +470,19 @@ comments: true
=== "Ruby"
```ruby title="binary_search_edge.rb"
[class]{}-[func]{binary_search_right_edge}
### 二分查找最右一个 target ###
def binary_search_right_edge(nums, target)
# 转化为查找最左一个 target + 1
i = binary_search_insertion(nums, target + 1)
# j 指向最右一个 target ,i 指向首个大于 target 的元素
j = i - 1
# 未找到 target ,返回 -1
return -1 if j == -1 || nums[j] != target
j # 找到 target ,返回索引 j
end
```
=== "Zig"
@@ -293,7 +293,26 @@ comments: true
=== "Ruby"
```ruby title="binary_search_insertion.rb"
[class]{}-[func]{binary_search_insertion_simple}
### 二分查找插入点(无重复元素) ###
def binary_search_insertion_simple(nums, target)
# 初始化双闭区间 [0, n-1]
i, j = 0, nums.length - 1
while i <= j
# 计算中点索引 m
m = (i + j) / 2
if nums[m] < target
i = m + 1 # target 在区间 [m+1, j] 中
elsif nums[m] > target
j = m - 1 # target 在区间 [i, m-1] 中
else
return m # 找到 target ,返回插入点 m
end
end
i # 未找到 target ,返回插入点 i
end
```
=== "Zig"
@@ -625,7 +644,26 @@ comments: true
=== "Ruby"
```ruby title="binary_search_insertion.rb"
[class]{}-[func]{binary_search_insertion}
### 二分查找插入点(存在重复元素) ###
def binary_search_insertion(nums, target)
# 初始化双闭区间 [0, n-1]
i, j = 0, nums.length - 1
while i <= j
# 计算中点索引 m
m = (i + j) / 2
if nums[m] < target
i = m + 1 # target 在区间 [m+1, j] 中
elsif nums[m] > target
j = m - 1 # target 在区间 [i, m-1] 中
else
j = m - 1 # 首个小于 target 的元素在区间 [i, m-1] 中
end
end
i # 返回插入点 i
end
```
=== "Zig"
@@ -228,7 +228,17 @@ comments: true
=== "Ruby"
```ruby title="two_sum.rb"
[class]{}-[func]{two_sum_brute_force}
### 方法一:暴力枚举 ###
def two_sum_brute_force(nums, target)
# 两层循环,时间复杂度为 O(n^2)
for i in 0...(nums.length - 1)
for j in (i + 1)...nums.length
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end
```
=== "Zig"
@@ -531,7 +541,19 @@ comments: true
=== "Ruby"
```ruby title="two_sum.rb"
[class]{}-[func]{two_sum_hash_table}
### 方法二:辅助哈希表 ###
def two_sum_hash_table(nums, target)
# 辅助哈希表,空间复杂度为 O(n)
dic = {}
# 单层循环,时间复杂度为 O(n)
for i in 0...nums.length
return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])
dic[nums[i]] = i
end
[]
end
```
=== "Zig"