304. 最大乘积

304. 最大乘积

 
给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大

样例

输入: nums = [3,4,1,2]
输出: 24

注意事项

  • nums.size <= 500000 n u m s . s i z e < = 5 0 0 0 0 0
  • -10000 <= nums[i] <= 10000 1 0 0 0 0 < = n u m s [ i ] < = 1 0 0 0 0
 
 
public class Solution {
    /**
     * @param nums: Unordered array
     * @return: return the largest product
     */
    public long MaximumProduct(int[] nums) {
        // write your code here
         long a = Long.MIN_VALUE;
            long b = Long.MIN_VALUE;
            long c = Long.MIN_VALUE;
            long d = Long.MAX_VALUE;
            long e = Long.MAX_VALUE;
 
 
            for (int num : nums) {
                if (num > 0) {
                    if (num > a) {
                        c = b;
                        b = a;
                        a = num;
                    } else if (num > b) {
                        c = b;
                        b = num;
                    } else if (num > c) {
                        c = num;
                    }
                } else {
                    if (num < d) {
                        e = d;
                        d = num;
                    } else if (num < e) {
                        e = num;
                    }
                }
            }
            return a * Math.max(b * c, d * e);
    }
}
 
 
 
public class Solution {
    /**
     * @param nums: Unordered array
     * @return: return the largest product
     */
    public long MaximumProduct(int[] nums) {
        // write your code here
         for (int i = 0; i < 3; i++) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[i] < nums[j]) {
                        int temp = nums[j];
                        nums[j] = nums[i];
                        nums[i] = temp;
                    }
                    if (nums[nums.length - 1 - i] > nums[j - i]) {
                        int temp = nums[j - i];
                        nums[j - i] = nums[nums.length - 1 - i];
                        nums[nums.length - 1 - i] = temp;
                    }
                }
            }
            long a = nums[1] * nums[2];
            long b = nums[nums.length - 1] * nums[nums.length - 2];
        return nums[0] * Math.max(a, b);
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这是一个FIR低筒滤波器吗wire[7:0] filter_coefficient1 = 8'd7;   28.wire[7:0] filter_coefficient2 = 8'd5; 29.wire[7:0] filter_coefficient3 = 8'd51; 30.wire[7:0] filter_coefficient4 = 8'd135; 31.wire[7:0] filter_coefficient5 = 8'd179; 32.wire[7:0] filter_coefficient6 = 8'd135; 33.wire[7:0] filter_coefficient7 = 8'd51; 34.wire[7:0] filter_coefficient8 = 8'd5; 35.wire[7:0] filter_coefficient9 = 8'd7; 36. 37.定义 9 个有符号整数型的寄存器变量,保存乘积结果 38.reg signed [16:0] result1=17'b0 ; 39.reg signed [16:0] result2=17'b0 ; 40.reg signed [16:0] result3=17'b0 ; 41.reg signed [16:0] result4=17'b0 ; 42.reg signed [16:0] result5=17'b0 ; 43.reg signed [16:0] result6=17'b0 ; 44.reg signed [16:0] result7=17'b0 ; 45.reg signed [16:0] result8=17'b0 ; 46.reg signed [16:0] result9=17'b0 ; 47. FIR 滤波器的输出值 48.always@(posedge clk_sample)  49.      begin 50.          result1 <= cascade delay1*filter_coefficient1 ;  51.          result2 <= cascade delay2*filter_coefficient2 ;   52.          result3 <= cascade delay3*filter_coefficient3 ;  53.          result4 <= cascade delay4*filter_coefficient4 ; 54.          result5 <= cascade delay5*filter_coefficient5 ; 55.          result6 <= cascade delay6*filter_coefficient6 ; 56.          result7 <= cascade delay7*filter_coefficient7; 57.          result8 <= cascade delay8*filter_coefficient8; 58.          result9 <= cascade delay9*filter_coefficient9 ; 59.          output_data <= result1 + result2 + result3 +  60.          result4 +result5 + result6 + result7 + 61.           result8 + result9 ; 62.          end          
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代我西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值