zoukankan      html  css  js  c++  java
  • 122. Best Time to Buy and Sell Stock II

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

    Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    解题思路:

    贪心求最大值

    1. class Solution {  
    2. public:  
    3.     int maxProfit(vector<int>& prices) {  
    4.         if(prices.size()==0||prices.size()==1) return 0;  
    5.           
    6.         int max_profit=0;  
    7.           
    8.         for(int i=0;i<prices.size()-1;i++){  
    9.             if(prices[i+1]-prices[i]>0) max_profit+=(prices[i+1]-prices[i]);        
    10.         }  
    11.         return max_profit;  
    12.           
    13.     }  
    14. };  
  • 相关阅读:
    增加文章
    网站之注册
    C#常用的引用
    Session.Abandon和Session.Clear有何不同 (转)
    C#文件路径的写法
    UpdatePanel的用法详解
    [转]asp:ScriptManager
    Git 常用命令
    AJAX请求 $.post方法的使用
    a 标签中调用js的几种方法
  • 原文地址:https://www.cnblogs.com/liangyc/p/8819903.html
Copyright © 2011-2022 走看看