题解 | 买卖股票的最好时机(一)
买卖股票的最好时机(一)
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
if (prices.length <= 1) {
return 0;
}
int start = 0, end = 1;
int res = 0;
while (end < prices.length) {
if (prices[end] > prices[start]) {
int tmp = prices[end] - prices[start];
res = Math.max(res, tmp);
} else {
start = end;
}
end++;
}
return res;
}
}
查看30道真题和解析