20240804 Finished
This commit is contained in:
parent
c935b56fbb
commit
0a058a3277
|
@ -194,3 +194,4 @@ mod p172_factorial_trailing_zeroes;
|
|||
mod p69_sqrtx;
|
||||
mod p50_powx_n;
|
||||
mod p149_max_points_on_a_line;
|
||||
mod p70_climbing_stairs;
|
42
src/problem/p70_climbing_stairs.rs
Normal file
42
src/problem/p70_climbing_stairs.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* [70] Climbing Stairs
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn climb_stairs(n: i32) -> i32 {
|
||||
if n == 1 {
|
||||
return 1;
|
||||
} else if n == 2 {
|
||||
return 2;
|
||||
}
|
||||
|
||||
let mut last_last = 1;
|
||||
let mut last = 2;
|
||||
|
||||
for i in 3..=n {
|
||||
let now = last + last_last;
|
||||
last_last = last;
|
||||
last = now;
|
||||
}
|
||||
|
||||
last
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_70() {
|
||||
assert_eq!(2, Solution::climb_stairs(2));
|
||||
assert_eq!(3, Solution::climb_stairs(3));
|
||||
assert_eq!(5, Solution::climb_stairs(4));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user