Re: [閒聊] 每日LeetCode已回收
※ 引述《JerryChungYC (JerryChung)》之銘言:
: https://leetcode.com/problems/climbing-stairs
: 70. Climbing Stairs
: 爬樓梯,要花 n 步
: 每次只能走 1 步或 2 步,問有幾種走法?
: 簡單來說就是費氏數列
: Python3 code:
: ------------------------------------------------------
: class Solution:
: def climbStairs(self, n: int) -> int:
: a, b = 1, 2
: for i in range(n-1):
: a, b = b, a + b
: return a
: ------------------------------------------------------
以前菜逼C++寫的Code很繁瑣 還用了一個超大Array
換成比較簡潔的Rust Style
Code:
impl Solution {
pub fn climb_stairs(n: i32) -> i32 {
let (mut a, mut b) = (1, 2);
for _ in 3..=n {
let next = a + b;
a = b;
b = next;
}
if n == 1 {a} else {b}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.48.97 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1705555579.A.72B.html
→
01/18 13:26,
1年前
, 1F
01/18 13:26, 1F
推
01/18 19:16,
1年前
, 2F
01/18 19:16, 2F
討論串 (同標題文章)