leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客
原文地址
题目地址
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).
解答(Java):
public int maxProfit(int[] prices) {
int maxPro = 0;
int length = prices.length - 1;
for (int i = 0; i < length; i++) {
if (prices[i] < prices[i + 1]) {
maxPro += prices[i + 1] - prices[i];
}
}
return maxPro;
}
leetcode:122. Best Time to Buy and Sell Stock II(java)解答的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
随机推荐
- 下划线hover下动态出现技巧
酷炫的动画效果往往更能吸引眼球,下面我将分享纯CSS中,hover的时候出现下划线动态飞入的技巧. 1.下划线从左侧飞入: div::before{ content:""; wid ...
- 洛谷P1035 级数求和
#include <iostream> using namespace std; int main(){ long k,i; cin >> k; double s=0.0; ; ...
- MySQL redo log 与 binlog 的区别
MySQL redo log 与 binlog 的区别 什么是redo log 什么是binlog redo log与binlog的区别 1. 什么是redo log? redo log又称重做日志文 ...
- 24. TABLES
24. TABLES TABLES表提供有关数据库中表的信息. TABLES表有以下列: TABLE_CATALOG :表所属目录的名称.该值始终为def. TABLE_SCHEMA :表所属sche ...
- 如何禁用python警告
有-W选项. python -W ignore foo.py 所属网站分类: python基础 > 综合&其它 作者:jiem 链接:http://www.pythonheidong.c ...
- EmpireofCode文档翻译 https://empireofcode.com/game/
In Campaign mode, you can check your strategies on already defeated bases. You will not lose your tr ...
- POJ 1252 Euro Efficiency(最短路 完全背包)
题意: 给定6个硬币的币值, 问组成1~100这些数最少要几个硬币, 比如给定1 2 5 10 20 50, 组成40 可以是 20 + 20, 也可以是 50 -10, 最少硬币是2个. 分析: 这 ...
- 把以100000+4位随机码的登录账号(比如1000001234),赋予制单页面的权限,怎么写sql啊
insert into sys_user_role (user_id,role_id,office_id) select id,'000101100000000004UP',company_id f ...
- XV6锁
锁 xv6 运行在多处理器上,即计算机上有多个单独执行代码的 CPU.这些 CPU 操作同一片地址空间并分享其中的数据结构:xv6 必须建立一种合作机制防止它们互相干扰.即使是在单个处理器上,xv6 ...
- zoj 1763 A Simple Question of Chemistry
A Simple Question of Chemistry Time Limit: 2 Seconds Memory Limit: 65536 KB Your chemistry lab ...