题解 | #最小花费爬楼梯# | Rust
最小花费爬楼梯
https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e
use std::io::{self, *}; struct Solution{ costs: std::vec::Vec<i32>, n: i32 } impl Solution{ fn minCosts(&self) -> i32 { let mut c : i32 = 0; if self.n <= 1 { return c; } let (mut cost_one, mut cost_two) = (self.costs[0], self.costs[1]); for i in 2..self.n as usize { c = std::cmp::min(cost_one, cost_two); cost_one = cost_two; cost_two = c + self.costs[i]; } return std::cmp::min(cost_one, cost_two); } } fn main() { let mut n:i32 = 0; let mut input = String::new(); io::stdin().read_line(&mut input); n = input.trim().parse().unwrap(); let mut input = String::new(); io::stdin().read_line(&mut input); let numbers: Vec<i32> = input.split_whitespace() .map(|s| s.parse().expect("parse error")) .collect(); print!("{}", Solution{costs:numbers, n:n}.minCosts()); }