题解 | #买卖股票的最好时机(一)# | Rust

买卖股票的最好时机(一)

https://www.nowcoder.com/practice/351b87e53d0d44928f4de9b6217d36bb

use std::io::{self, *};

struct Solution {
    prices : std::vec::Vec<i32>
}

impl Solution {
    fn new(p : std::vec::Vec<i32>) -> Self{
        Solution {prices : p}
    }

    fn getMaxProfit(&self) -> i32 {
        let mut max_profit : i32 = 0;
        let mut min_p : i32 = self.prices[0];
        for num in &self.prices {
            max_profit = std::cmp::max(max_profit, *num - min_p);
            min_p = std::cmp::min(*num, min_p);
        }
        return max_profit;
    }
}

fn main() {
    let mut n:i32 = 0;
    let mut input = String::new();
    io::stdin().read_line(&mut input);
    n = input.trim().parse().unwrap();
    io::stdin().read_line(&mut input);
    let numbers: std::vec::Vec<i32> = input.split_whitespace()
        .map(|s| s.parse().expect("parse error"))
        .collect();
    print!("{}", Solution::new(numbers).getMaxProfit());
}

全部评论

相关推荐

彧未sr:查看图片
投递牧原集团等公司8个岗位
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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