一次遍历,维护最小价格和最大利润
import java.util.*;
public class Solution {
/**
*
* @param prices int整型一维数组
* @return int整型
*/
public int maxProfit (int[] prices) {
if(prices.length == 0){
return 0;
}
int max=0,min=prices[0],io;
for(int i = 0;i<prices.length;i++){
if(min> prices[i]){
min = prices[i];
}
else if(max < prices[i] - min){
max = prices[i] - min;
}
}
return max;
}
}