leetcode121 Best Time to Buy and Sell Stock

说白了找到最大的两组数之差即可

 class Solution {
public:
int maxProfit(vector<int>& prices) {
int m = ;
for(int i = ; i < prices.size(); i++){
for(int j = i + ; j < prices.size(); j++){
m = max(m, prices[j] - prices[i]);
}
}
return m;
}
};

leetcode122 Best Time to Buy and Sell Stock II

关键在于明白股票可以当天卖出再买进

 class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() == ) return ;
int m = ;
for(int i = ; i < prices.size() - ; i++){
if(prices[i] < prices[i + ]){
m += prices[i + ] - prices[i];
}
} return m;
}
};

leetcode 买卖股票问题的更多相关文章

  1. LeetCode 买卖股票的最佳时机 II

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次 ...

  2. LeetCode 买卖股票的最佳时机

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  3. leetcode题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  4. 【Leetcode】【简单】【122. 买卖股票的最佳时机 II】【JavaScript】

    题目描述 122. 买卖股票的最佳时机 II 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票) ...

  5. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

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

  7. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. K8s+jenkins实现提升效率 —— 一些小记录

    尝试下K8s + jenkins的组合,非常方便.在这里记录一下: kubernetes版本: 1.10 + deployment.yaml apiVersion: v1 kind: Service ...

  2. cmd命令往MySQL数据库提交数据

    第一步:MySQL -V检查下载成功否第二步:mysql -u root -p 登陆密码第三步:创建一个数据库 create database if not exists 数据库name: 第四步:展 ...

  3. Linux 安装 Rabbit MQ

    安装socat yum -y install socat 下载erlang软件包,本文使用erlang-19.0.4版本,下面给出下载链接 wget http://www.rabbitmq.com/r ...

  4. element-- 修改MessageBox 弹框 中确定和取消按钮顺序

    需求:修改弹框中的 取消/确定按钮顺序,及头部和底部背景颜色; 原ui效果图 需求ui效果图 方法:对取消及确定按钮自定义类名,样式重写

  5. Vue 给axios做个靠谱的封装(报错,鉴权,跳转,拦截,提示)

    需求及实现 统一捕获接口报错 弹窗提示 报错重定向 基础鉴权 表单序列化 用法及封装 用法 // 服务层 , import默认会找该目录下index.js的文件,这个可能有小伙伴不知道可以去了解npm ...

  6. SQL server 2012安装中出现的INSTALLSHAREDDIR 和 INSTALLSHAREDWOWDIR 参数具有相同的值问题

    出现的问题如下: INSTALLSHAREDDIR 和 INSTALLSHAREDWOWDIR 参数具有相同的值“D:\soft\sql”.但是,这些参数必须具有不同的值.请为其中一个参数指定不同的值 ...

  7. laravel5.5 env

    env 函数 读取的变量里面带有 # 号的情况下 数据会丢失

  8. react 入坑之罪

    componentDidMount :生命周期在react下只调用一次, render:比它先执行 componentWillRecvieProps(newProps) :能取到父组件的值 rende ...

  9. styled-components 背后的魔法

    styled-components 定义组件的风格为 const Button = styled.button` background-color: papayawhip; border-radius ...

  10. Java中的异常处理与抛出

    一.异常处理 程序运行过程中出现的,导致程序无法继续运行的错误叫做异常. Java中有多种异常,所有异常的父类是Throwable,他主要有两个子类Error和Exception. Error一般是J ...