题目链接:https://www.lintcode.com/problem/best-time-to-buy-and-sell-stock/description
有点惭愧,这个代码我本地测了几组用例应该是没问题的,但是OJ报了WA,先贴一下代码吧。
const INT_MAX = int(^uint(0) >> 1);
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
func maxProfit (prices []int) int {
// write your code here
if (prices == nil || len(prices) == 0) {
return 0;
}
var min int = INT_MAX;
var i, profit int;
profit = 0;
for i = 0; i < len(prices); i++ {
if min > prices[i] {
min = prices[i];
}
if profit < (prices[i] - min) {
profit = prices[i] - min;
}
}
return profit;
}
func main() {
var prices1 = []int{3, 2, 3, 1, 2};
var profit1 int = maxProfit(prices1);
fmt.Println(profit1);
}