题解 | #买卖股票的最好时机(一)# | 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()); }