题解 | #最小花费爬楼梯# | 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());
}

#Rust#
全部评论

相关推荐

2025-12-15 19:41
已编辑
有赞_前端开发工程师
smile丶snow:尽量保证的一张a4纸吧。为什么工作经历只有公司?如果项目经历是工作里面的,应该写到工作经历里的吧。没有写项目职责,项目全是一个人写的吗?标题大小也好怪。两个项目的分割看不出来是,它和那些项目成功字体是一样大的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务