力扣 122.买卖股票的最佳时机 II


在这里插入图片描述
代码:

class Solution {
   
public:
    int maxProfit(vector<int>& prices) {
   
        if(prices.size()==1) return 0;
        int res = 0;
        int i=0;
        while(i<prices.size()-1){
   
            int j=i+1;
            if(prices[j]>prices[i]){
   //在找到对应元素的下一个元素比他大的时候买入
                while(j+1 < prices.size()){
   
                    if(prices[j+1]>=prices[j]) j++;//找到最大的
                    else break;
                }
                res=res+prices[j]-prices[i];//记录利润
                i=j+1;
            }
            else{
   
                i++;
            }
        }
        return res;
    }
};

在这里插入图片描述

相关推荐

  1. 122. 买卖股票最佳时机 II

    2024-01-31 17:52:03       36 阅读
  2. 122. 买卖股票最佳时机 II

    2024-01-31 17:52:03       31 阅读
  3. [题解]122. 买卖股票最佳时机 II

    2024-01-31 17:52:03       13 阅读
  4. 123. 买卖股票最佳时机 III

    2024-01-31 17:52:03       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-31 17:52:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-31 17:52:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-31 17:52:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-31 17:52:03       20 阅读

热门阅读

  1. Kotlin开发中有关时间的具体使用

    2024-01-31 17:52:03       33 阅读
  2. Golang中的方法链

    2024-01-31 17:52:03       28 阅读
  3. 本周黄金价格将面临重大风险事件

    2024-01-31 17:52:03       42 阅读
  4. LRU(Least Recently Used)

    2024-01-31 17:52:03       33 阅读
  5. MySQL 索引 create index 详解

    2024-01-31 17:52:03       42 阅读
  6. linux nodejs无法安装canvas模块

    2024-01-31 17:52:03       32 阅读
  7. c#常用的修饰符

    2024-01-31 17:52:03       36 阅读
  8. C代码,控制亮灯的代码

    2024-01-31 17:52:03       32 阅读
  9. 马可波罗API中的item_get方法详解

    2024-01-31 17:52:03       46 阅读