LeetCode-121. Best Time to Buy and Sell Stock
来源:互联网 发布:excel如何去除重复数据 编辑:程序博客网 时间:2024/06/10 22:42
Description:
Example 1:
Example 2:
Solution (C++):
算法分析:
这道题和我之前做过的一道题LeetCode-122. Best Time to Buy and Sell Stock II是一个系列的。当时不太懂题目的意思,现在明白了,就是说,这个数列是一只股票在第0天、第1天、第2天······第n天的价格。这道题的要求是我们买卖一次,然后要获得最大利润。那么其实就是要找到数列中差别最大的两个数,而且第一个数小,第二个数大。
那么具体的解法就是。假如现在是第i天。之前0~i天的股票价格中,最低价是minp。那么今天卖股票的利润就是prices[i]-minp.那么我们在每一天都和最大利润sell比较。自然就可以获得最大利润了。
程序分析:
程序没啥多说点。有一点:INI_MAX与INI_MIN这两个常量:
INT_MIN = -2^31INT_MAX = 2^31 - 1
这两个数分别表示int型最大数与最小数。32位运算器下。以后就不用写9999999了。
还有一点,有一个Error的地方就是,我初始化minp是通过:
minp=prices[0];
来完成的,但这样系统报错。后来改成99999999就没报错了。所以初始化还是以常量为好。
阅读全文
0 0
- LeetCode 121. Best Time to Buy and Sell Stock
- [Leetcode] 121. Best Time to Buy and Sell Stock
- [leetcode] 121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock
- [leetcode] 121. Best Time to Buy and Sell Stock
- 121. Best Time to Buy and Sell Stock LeetCode
- [LeetCode]121. Best Time to Buy and Sell Stock
- Leetcode 121. Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock
- LeetCode 121. Best Time to Buy and Sell Stock
- LeetCode题解:121. Best Time to Buy and Sell Stock
- LeetCode *** 121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock
- #leetcode#121. Best Time to Buy and Sell Stock
- LeetCode 121. Best Time to Buy and Sell Stock
- 121.[Leetcode]Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock
- Leetcode 121. Best Time to Buy and Sell Stock
- 自动化运维之ansible的ad-hoc
- 初次熟悉使用jenkins 持续集成
- 雅虎初始化css
- 【云星数据---mesos实战系列】:marathon实战Constraints篇-002
- Mistral 经典案例
- LeetCode-121. Best Time to Buy and Sell Stock
- 解决初学javaWeb者缺少Mysql数据库连接的Jar包引起的问题。
- LeetCode--Minimum Depth of Binary Tree
- Android获取APP的应用程序名称、包名、图标,版本号基本信息
- Java中的集合类Collection类
- 国家气象局提供的天气预报接口
- java 技巧
- idea maven reimport失效
- Eclipse编程习惯