# 题目

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

# 代码

public class Solution {
public int maxProfit(int[] prices) {

if (prices.length<=0 || prices.length==1) return 0;
int maxProfit=0;
int minTemp=prices[0];
for(int i=1;i<prices.length;i++){
if(prices[i]<minTemp){
minTemp=prices[i];
}
else{
if(prices[i]-minTemp>maxProfit){
maxProfit=prices[i]-minTemp;
}
}
}
return maxProfit;
}
}

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处

******************************************/

|

LeetCode 309. Best Time to B & S Stock with CD

68 0
|

LeetCode 188. Best Time to Buy and Sell Stock IV

71 0
|

LeetCode 123. Best Time to Buy and Sell Stock III

88 0
|

LeetCode 121. Best Time to Buy and Sell Stock

64 0
Leetcode-Easy 121. Best Time to Buy and Sell Stock
Leetcode-Easy 121. Best Time to Buy and Sell Stock
114 0
|

LeetCode 121 Best Time to Buy and Sell Stock（股票买入卖出的最佳时间）

854 0
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III

37 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构

33 4
|
1月前
|

【Leetcode刷题Python】牛客. 数组中未出现的最小正整数

60 2
|
1月前
|

【Leetcode刷题Python】从列表list中创建一颗二叉树

31 7