mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 23:46:29 +08:00
3f4220de81
* preorder, inorder, postorder -> pre-order, in-order, post-order * Bug fixes * Bug fixes * Update what_is_dsa.md * Sync zh and zh-hant versions * Sync zh and zh-hant versions. * Update performance_evaluation.md and time_complexity.md * Add @khoaxuantu to the landing page. * Sync zh and zh-hant versions * Add @ khoaxuantu to the landing page of zh-hant and en versions.
26 lines
481 B
Ruby
26 lines
481 B
Ruby
=begin
|
|
File: climbing_stairs_dfs.rb
|
|
Created Time: 2024-05-29
|
|
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|
=end
|
|
|
|
### 搜尋 ###
|
|
def dfs(i)
|
|
# 已知 dp[1] 和 dp[2] ,返回之
|
|
return i if i == 1 || i == 2
|
|
# dp[i] = dp[i-1] + dp[i-2]
|
|
dfs(i - 1) + dfs(i - 2)
|
|
end
|
|
|
|
### 爬樓梯:搜尋 ###
|
|
def climbing_stairs_dfs(n)
|
|
dfs(n)
|
|
end
|
|
|
|
### Driver Code ###
|
|
if __FILE__ == $0
|
|
n = 9
|
|
|
|
res = climbing_stairs_dfs(n)
|
|
puts "爬 #{n} 階樓梯共有 #{res} 種方案"
|
|
end
|