转载请注明出处: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)解答的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

随机推荐

  1. IOS学习笔记1—Iphone程序运行流程

    Iphone程序运行流程 main.m文件,iOS应用程序的主入口 main函数的两个参数为命令行参数,在ios开发中不会用到这些元素,包括这两个参数是为了与标准ANSI C保持一致 UIApplic ...

  2. C++11程序设计要点总结-模板机制详解

    C++程序设计要点总结 在编程的过程中呢我们总会遇到一些各种各样的问题,就比如在写方法的时候,我们一个同样的方法要写好几种类型的呢,这让我们很伤脑筋,但是呢C++有一个强大的功能就是模板机制,这个模板 ...

  3. c++的if语句中的110为什么不等于110?

    从上图可以看出,当表达式1.1*x被直接放进if的判断括号中时1.1*x不等于y,但是将1.1*x赋值给z时,z与y相等,这是为什么?(以下为不等价时的代码) #include<stdio.h& ...

  4. java用XSSFWorkbook实现读写Excel

    /** * 读取Excel文件的内容 * @param inputStream excel文件,以InputStream的形式传入 * @param sheetName sheet名字 * @retu ...

  5. Python自动化测试框架——概述

    #使用import import unittest #测试用例TestCase ''' 一个测试用例时一个完整的测试流程,包括了环境准备SetUp,测试执行Run,测试环境还原TearDown 一个测 ...

  6. Flask--init和run启动研究---xunfeng巡风实例篇

    第一: 首先在view目录下的__init__.py文件定义好 (1) Flask实例 : app = Flask(__name__) (2) 数据库实例 Mongo = Conn.MongoDB(a ...

  7. asp.net 页面缓存、数据缓存

    页面缓存,webform框架的aspx页面,服务器引擎生成的页面可以被缓存.

  8. POJ 1300 最基础的欧拉回路问题

    题目大意: 从0~n-1编号的房间,从一个起点开始最后到达0号房间,每经过一扇门就关上,问最后能否通过所有门且到达0号房间 我觉得这道题的输入输出格式是我第一次遇到,所以在sscanf上也看了很久 每 ...

  9. 【随机化算法】codeforces Matrix God

    http://codeforces.com/gym/101341 [题意] 给定三个方阵A,B,C,问AB=C是否成立? 方阵的规模最大为1000 [思路] 求AB的时间复杂度为n*n*n,会超时 左 ...

  10. 售货员的难题(codevs 2596)

    题目描述 Description 某乡有n个村庄(1<n<=15),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且A村到B村与B村到A村 ...