题解 | #最小花费爬楼梯# | 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#
全部评论

相关推荐

10-15 15:00
潍坊学院 golang
跨考小白:这又不是官方
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务