C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3726 访问。
给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。
输入: [1,2,3]
输出: 6
输入: [1,2,3,4]
输出: 24
注意:
给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]。
输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。
Given an integer array, find three numbers whose product is maximum and output the maximum product.
Input: [1,2,3]
Output: 6
Input: [1,2,3,4]
Output: 24
Note:
The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3726 访问。
public class Program {public static void Main(string[] args) {int[] nums = null;nums = new int[] { -4, 12, 9, -20 };var res = MaximumProduct(nums);Console.WriteLine(res);Console.ReadKey();}private static int MaximumProduct(int[] nums) {//先排序Array.Sort(nums);//记录数组长度int length = nums.Length;//该题主要考查数组中包含负数的情况//如果全为正数,排序后输出后3位的乘积即可//否则,最大值一定在最小的2个负数和最大数的乘积、后3位值的乘积中产生//直接比较它们的大小输出即可//先存下数组后3位值的乘积int product = nums[length - 1] * nums[length - 2] * nums[length - 3];//取前2位和最后1位的乘积、后3位值的乘积中的最大值product = Math.Max(product, nums[length - 1] * nums[1] * nums[0]);//输出这个最大值return product;}}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3726 访问。
960
分析:
显而易见,以上算法的时间复杂度基于 Array.Sort() 所使用的排序算法。
C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)相关推荐
- LeetCode刷题笔记- 15.三数之和
LeetCode刷题笔记- 15.三数之和 C语言 题目 注意点 C语言 /*** Return an array of arrays of size *returnSize.* The sizes ...
- leetcode 628. Maximum Product of Three Numbers | 628. 三个数的最大乘积(Java)
题目 https://leetcode-cn.com/problems/maximum-product-of-three-numbers/ 题解 找到三个数的最大乘积.先给数组排序,考虑可能有负数的情 ...
- 【leetcode刷题】21.三数之和——Java版
⭐欢迎订阅<leetcode>专栏,每日一题,每天进步⭐ 一题二写,三数之和,题解四瞅五瞄六瞧,水平还七上八下九流,十分辣鸡. --leetcode此题热评 前言 哈喽,大家好,我是一条. ...
- leetcode(3)——414. 第三大的数(C++中的 set,::作用符号,迭代器),628 三个数的最大乘积(sort函数的用法)
文章目录 前言 414 程序 疑惑 set ::在c++中什么意思 一.作用域符号: 二.全局作用域符号: 三.作用域分解运算符: C++ 迭代器 628 程序 疑惑 sort函数 前言 大佬的程序, ...
- LeetCode 628. 三个数的最大乘积
1. 题目 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6示例 2: 输入: [1,2,3,4] 输出: 24注意: 给定的整型 ...
- leetcode 628. 三个数的最大乘积(排序)
给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 解题思路 最大的乘积可能有两种情况 1.两个最小负数和一个最大正数 2.三个最大 ...
- 力扣628. 三个数的最大乘积
题目描述 给你一个整型数组 nums ,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 示例 1:输入:nums = [1,2,3] 输出:6示例 2:输入:nums = [1,2,3,4 ...
- Leetcode刷题总结(三)
1.不用加号的加法 思路:不能用算术运算符,因此考虑位运算来实现加法. class Solution { public:int add(int a, int b) {int sum=0;int car ...
- C#LeetCode刷题之#191-位1的个数(Number of 1 Bits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4052 访问. 编写一个函数,输入是一个无符号整数,返回其二进制表 ...
- C#LeetCode刷题之#643-子数组最大平均数 I( Maximum Average Subarray I)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3728 访问. 给定 n 个整数,找出平均数最大且长度为 k 的连 ...
最新文章
- mysql常用linux命令大全_Linux-MySQL常用命令(示例代码)
- 线性规划的原始对偶算法
- Android Studio安卓开发中使用json来作为网络数据传输格式
- python项目超级大脑-python项目之超级大脑
- 用动画实现android app启动界面的渐变效果
- Centos下Yum安装PHP5.5,5.6,7.0
- [c#]RabbitMQ的简单使用
- Oracle Rman 命令详解(List report backup configure)
- cf1562E. Rescue Niwen!
- (5)呼吸灯systemverilog与VHDL编码
- android 检测摇一摇
- python机器学习之决策树详解
- 电动汽车动力系统整车仿真,具有双向DCDC变换器实现能量反馈,可带异步电机
- ccs软件5.5安装
- PHP 数字转换为大写的函数
- 开放微博社区使用OAUTH协议简介
- python脚本编程:批量压缩图片大小
- UOS系统的Qt无法使用中文输入法
- Python爬虫:使用线程池快速下载视频
- 解决cannot resolve directory问题