leetcode-121 发表于 2019-02-25 | 分类于 leetcode | 阅读次数: 字数统计: 125 | 阅读时长 ≈ 1 这题使用双指针法 12345678910def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ max_profit, min_price = 0, float('inf') for price in prices: min_price = min(min_price, price) max_profit = max(max_profit, price - min_price) return max_profit 123456789101112class Solution {public: int maxProfit(vector<int>& prices) { int maxPro = 0; int minPrice = INT_MAX; for(int i = 0; i < prices.size(); i++){ minPrice = min(minPrice, prices[i]); maxPro = max(maxPro, prices[i] - minPrice); } return maxPro; }}; 123456789class Solution {public: int maxProfit(vector<int>& prices) { int res=0,buyp=INT_MAX; for(auto i:prices) (i<buyp)?(buyp=i):(res=max(i-buyp,res)); return res; }}; 更加简洁的cpp