Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
maximum-product-of-three-numbers.cpp 896 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2017-07-03 22:47 +08:00 . add
// Time: O(n)
// Space: O(1)
class Solution {
public:
int maximumProduct(vector<int>& nums) {
auto min1 = numeric_limits<int>::max();
auto min2 = numeric_limits<int>::max();
auto max1 = numeric_limits<int>::min();
auto max2 = numeric_limits<int>::min();
auto max3 = numeric_limits<int>::min();
for (const auto& n: nums) {
if (n <= min1) {
min2 = min1;
min1 = n;
} else if (n <= min2) {
min2 = n;
}
if (n >= max1) {
max3 = max2;
max2 = max1;
max1 = n;
} else if (n >= max2) {
max3 = max2;
max2 = n;
} else if (n >= max3) {
max3 = n;
}
}
return max(min1 * min2 * max1, max1 * max2 * max3);
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助