Leetcode 122 Best Time to Buy and Sell Stock II 贪心
用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。交易次数不限,但一次只能交易一支股票,也就是说手上最多只能持有一支股票,求最大收益。
关键:能赚就赚
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans = ;
for(int i = ; i<prices.size(); ++i){
ans += (prices[i] > prices[i-])?
prices[i] - prices[i-]: ;
}
return ans;
}
};
Leetcode 122 Best Time to Buy and Sell Stock II 贪心的更多相关文章
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 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 al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 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 ...
- LeetCode 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 ...
- [leetcode]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 al ...
- Java for LeetCode 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 al ...
随机推荐
- Android 自己定义ViewGroup 实战篇 -> 实现FlowLayout
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38352503 .本文出自[张鸿洋的博客] 1.概述 上一篇已经基本给大家介绍了怎 ...
- DOCKER学习心得
原文:DOCKER学习心得 前言: Docker的主要学习心得来源于<docker技术入门与实战> --2019.1.1->2019.1.5 la 着重从基础部分--实例分析-- ...
- HDU 1069 Monkey and Banana DP LIS
http://acm.hdu.edu.cn/showproblem.php?pid=1069 题目大意 一群研究员在研究猴子的智商(T T禽兽啊,欺负猴子!!!),他们决定在房顶放一串香蕉,并且给猴子 ...
- 每天一个JavaScript实例-处理textarea中的字符成每一行
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Java验证是否为纯数字
package rbq.codedemo; import java.util.regex.Pattern; /** * Created by rbq on 2016/12/13. */ public ...
- Snmp常用oid
http://blog.csdn.net/youngqj/article/details/7311849 系统参数(1.3.6.1.2.1.1) OID 描述 备注 请求方式 .1.3.6.1.2 ...
- 小强的HTML5移动开发之路(45)——汇率计算器【1】
这两天看了<PhoneGap实战>上面有一个汇率计算器的例子,个人觉得比较好,就拿出来和大家分享一下,在接下来的几篇文章中我们来一起完成这个PhoneGap + Jquery mobile ...
- 【37%】【poj1436】Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5200 Accepted: 1903 Description There ...
- 【t084】数列
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一个数列定义如下:f(1) = 1,f(2) = 1,f(n) = (A * f(n - 1) + B ...
- [NPM] Run npm scripts in parallel
In this lesson we will look at running several npm scripts in parallel. Sometimes you don’t need scr ...