mirror of
https://github.com/krahets/hello-algo.git
synced 2025-08-02 11:33:18 +08:00
build
This commit is contained in:
@ -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"
|
||||
|
@ -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"
|
||||
|
Reference in New Issue
Block a user