题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
https://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
import java.util.*;
public class Solution {
/**
*
* @param prices int整型一维数组
* @return int整型
*/
public int maxProfit (int[] prices) {
// write code here
int min = prices[0];
int profit = 0;
int n = prices.length;
if(n <= 1){
return 0;
}
for(int i = 1; i < n; i++){
profit = Math.max(profit, prices[i] - min);
if(prices[i] < min){
min = prices[i];
}
}
return profit;
}
}
算法题收录 文章被收录于专栏
收录所有算法题目解法

查看14道真题和解析