5.22
public class Solution {
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] prices) {
// write your code here
int max = 0;
int length = prices.length;
if(length <= 1){
return 0;
}
for(int i = 0;i < length -1;i++){
for(int j = i+1;j < length;j++){
int tmp = prices[j] - prices[i];
if(tmp > max){
max = tmp;
}
//System.out.println(i +"," + j + ",tmp:" + tmp);
}
}
return max;
}
}
本文介绍了一种通过双重循环遍历数组来寻找股票买卖最佳时机的算法,旨在找到能够获得最大利润的买入和卖出时间点。

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



