国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

leecode 解題總結:122. Best Time to Buy and Sell Stock II

2019-11-08 19:46:01
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>using namespace std;/*問題: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 multipletransactions at the same time (ie, you must sell the stock before you buy again).分析:此題的意思是允許多次買入后再拋出股票。每次買入股票前必須先拋出之前買的。舉例:0 6 -3 7,可以直接先計算相鄰兩個元素的差值,將所有大于0的差值累加起來輸入:52 4 1 7 1140 6 -3 744 3 2 1輸出:12160*/class Solution {public:    int maxProfit(vector<int>& prices) {        if(prices.empty())		{			return 0;		}		int size = prices.size();		int diff;		int maxSum = 0;		for(int i = 1 ; i < size ; i++)		{			diff = prices.at(i) - prices.at(i-1);			if(diff > 0)			{				maxSum += diff;			}		}		return maxSum;    }};void print(vector<int>& result){	if(result.empty())	{		cout << "no result" << endl;		return;	}	int size = result.size();	for(int i = 0 ; i < size ; i++)	{		cout << result.at(i) << " " ;	}	cout << endl;}void process(){	 vector<int> nums;	 int value;	 int num;	 Solution solution;	 while(cin >> num )	 {		 nums.clear();		 for(int i = 0 ; i < num ; i++)		 {			 cin >> value;			 nums.push_back(value);		 }		 int result = solution.maxProfit(nums);		 cout << result << endl;	 }}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 综艺| 保靖县| 平远县| 新建县| 油尖旺区| 玉树县| 西城区| 洛川县| 德令哈市| 常山县| 唐海县| 兴文县| 石楼县| 济阳县| 荥经县| 卓资县| 浮山县| 牡丹江市| 东至县| 平陆县| 澄城县| 吴旗县| 阳谷县| 维西| 集贤县| 基隆市| 南丰县| 东丰县| 行唐县| 荣昌县| 伊金霍洛旗| 铜鼓县| 新巴尔虎右旗| 隆化县| 新巴尔虎右旗| 武冈市| 紫金县| 舟山市| 格尔木市| 涟源市| 星座|