Comments (3)
解题思路
- 遍历数组, 记录最大最小值的差值(能被覆盖).
代码
private static int func(int[] ints) {
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
int result = 0;
for (int i = 0; i < ints.length; i++) {
int v = ints[i];
// 判定最大最小值
if (min > v) {
min = v;
max = min; // 最小值刷新时, 重制最大值
} else if (max < v) {
max = v;
result = (result < (max - min)) ? (max - min) : result; // 最大值刷新时, 重新判断最大利润
}
}
return result;
}
from pisecespeng.record.me.
LeetCode题解
解题思路
- 暴力法, 找出数组中两个数字的最大差值
代码
public static int func(int prices[]) {
int maxprofit = 0;
for (int i = 0; i < prices.length - 1; i++) {
for (int j = i + 1; j < prices.length; j++) {
int profit = prices[j] - prices[i];
if (profit > maxprofit) {
maxprofit = profit;
}
}
}
return maxprofit;
}
from pisecespeng.record.me.
LeetCode题解
解题思路
- 遍历一次, 记录历史最低点价格, 且每天都计算以此价格卖出之后的收益.
代码
public static int func(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
from pisecespeng.record.me.
Related Issues (20)
- 两数相加 HOT 2
- 买卖股票的最佳时期2 HOT 3
- 二叉树的层序遍历 HOT 2
- 二叉树的最大深度 HOT 2
- 二进制求和 HOT 4
- 从尾到头打印链表 HOT 2
- 位1的个数 HOT 4
- 删除排序数组中的重复项 HOT 2
- 删除链表的倒数第N个节点 HOT 3
- 加一 HOT 2
- 反转字符串 HOT 2
- 反转链表 HOT 3
- 只出现一次的数字 HOT 2
- 合并两个有序数组 HOT 3
- 合并两个有序链表 HOT 2
- 回文数 HOT 2
- 回文链表 HOT 3
- 复制带随机指针的链表 HOT 2
- 外观数列 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pisecespeng.record.me.