628. Maximum Product of Three Numbers的C++解法
来源:互联网 发布:mac mini英雄联盟测评 编辑:程序博客网 时间:2024/05/30 22:46
牵扯到有负数就变得很麻烦,为了不分析那么多可能的情况我选择找出三个最大的正数和三个最小的负数然后遍历这六个数字的所有三种情况的组合找出最大的那个。
class Solution {public:int maximumProduct(vector<int>& nums) {if (nums.size() == 3) return nums[0] * nums[1] * nums[2];int a[6] = {-1001,-1001,-1001,1001,1001,1001 };int i;for (i = 0; i < nums.size(); i++){if ((nums[i]>a[0])&&(nums[i]>=0)) a[0] = nums[i];if (a[0] > a[1]) swap(a[0], a[1]);if (a[1] > a[2]) swap(a[1], a[2]);if ((nums[i] < a[3])&&(nums[i]<0)) a[3] = nums[i];if (a[3] < a[4]) swap(a[3], a[4]);if (a[4] < a[5]) swap(a[4], a[5]);}int max = -1000000000;for (int i = 0; i < 6; i++)if (abs(a[i]) <= 1000)for (int j = i + 1; j < 6; j++)if (abs(a[j]) <= 1000)for (int k = j + 1; k < 6; k++) if (abs(a[k]) <= 1000) if (a[i] * a[j] * a[k] > max) max = a[i] * a[j] * a[k];return max;}};看了一下题解之后恍然大悟,最大值要么是三个最大数的乘积,要么是两个最小输和一个最大数的乘积。最暴力的算法是排序然后比较这两种可能。然后再贴一个和我思路差不多但是比我更简洁的java代码。
public int maximumProduct(int[] nums) { int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE, min2 = Integer.MIN_VALUE; for (int n : nums) { if (n > max1) { max3 = max2; max2 = max1; max1 = n; } else if (n > max2) { max3 = max2; max2 = n; } else if (n > max3) { max3 = n; } if (n < min1) { min2 = min1; min1 = n; } else if (n < min2) { min2 = n; } } return Math.max(max1*max2*max3, max1*min1*min2); }我自己的也可以改,改过之后从前12%进入了前0.22%。
class Solution {public:int maximumProduct(vector<int>& nums) {if (nums.size() == 3) return nums[0] * nums[1] * nums[2];int a[5] = {-1001,-1001,-1001,1001,1001};int i;for (i = 0; i < nums.size(); i++){if ((nums[i]>a[0])) a[0] = nums[i];if (a[0] > a[1]) swap(a[0], a[1]);if (a[1] > a[2]) swap(a[1], a[2]);if ((nums[i] < a[3])) a[3] = nums[i];if (a[3] < a[4]) swap(a[3], a[4]);}return max(a[0]*a[1]*a[2],a[2]*a[3]*a[4]);}};
阅读全文
0 0
- 628. Maximum Product of Three Numbers的C++解法
- Maximum Product of Three Numbers问题解法
- leetcode[Maximum Product of Three Numbers]//待整理多种解法
- [leetcode]628. Maximum Product of Three Numbers
- leetcode#628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- leetcode: 628. Maximum Product of Three Numbers
- leetcode 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- [LeetCode] 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- 628. Maximum Product of Three Numbers
- [leetcode]628. Maximum Product of Three Numbers
- LeetCode 628. Maximum Product of Three Numbers
- 关于秒杀
- swing那点事
- JAVA垃圾回收详解(评价指标篇)
- 编程思想的理解(POP,OOP,SOA,AOP)
- Android Studio安装配置详细步骤(图文)
- 628. Maximum Product of Three Numbers的C++解法
- POJ 1862 Stripies 题解
- Linux: Intel-Virtualization Technology(VT)下的中断
- 拿代码量算绩效考核?别毁了程序员
- 【mysql】ERROR 1130: Host ’...′ is not allowed to connect to this MySQL server
- Android 内外存储说明
- sublime text3 emmet 插件 安装方法,快捷键
- 什么是Node.js?
- 操作系统——磁盘调度算法