LeetCode之maxProfitII

解法一:
int maxprofit2(vector &prices)  //Best Time to Buy and Sell Stock II 
	{
		//cout<=prices[i+1])
		{
			i++;
			if (i>=prices.size()-1)
			{return 0;
			}			
		}
		//i--;
		profit-=prices[i];
		while(prices[i]<=prices[i+1]) 
		{
			i++;
			if (i>=prices.size()-1)
			{return profit+=prices[i];
			}
		}
		profit+=prices[i];
		vector sub_prices;
		for (j=i+1;j<=prices.size()-1;j++)
		{
			sub_prices.push_back(prices[j]);
		}
		profit+=maxprofit2(sub_prices);
		return profit;
	}

解法二:

int maxprofit2(vector &prices)  //Best Time to Buy and Sell Stock II 
	{
		//cout<=prices[i+1])
		{
			i++;
			if (i>=prices.size()-1)
			{return 0;
			}			
		}
		//i--;
		profit-=prices[i];
		while(prices[i]<=prices[i+1]) 
		{
			i++;
			if (i>=prices.size()-1)
			{return profit+=prices[i];
			}
		}
		profit+=prices[i];
		vector sub_prices;
		for (j=i+1;j<=prices.size()-1;j++)
		{
			sub_prices.push_back(prices[j]);
		}
		profit+=maxprofit2(sub_prices);
		return profit;
	}


你可能感兴趣的:(LeetCode)