这道题比较简单,从左到右扫描,每次都那当前数减去左边的最小数和profix比较,而最小数也很好找。2ms
public class Solution {
public int maxProfit(int[] prices) {
int min=99999;
int profix =0;
for(int i=0;i<prices.length;i++){
profix = Math.max(profix,prices[i]-min);
if(prices[i]<min)
min = prices[i];
}
if(profix<0)return 0;
else return profix;
}
}
本文介绍了一种简单高效的算法来解决股票买卖问题,通过一次遍历即可找出最大利润,使用了动态规划的思想,并附带了一个2ms运行时间的Java实现。
831

被折叠的 条评论
为什么被折叠?



