u
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/maximum-product-subarray/?envType=study-plan-v2&envId=top-100-liked
|
||||
// 152. 乘积最大子数组
|
||||
//
|
||||
// 给你一个整数数组 `nums` ,请你找出数组中乘积最大的非空连续 子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
|
||||
// 测试用例的答案是一个 32-位 整数。
|
||||
// 请注意,一个只包含一个元素的数组的乘积是这个元素的值。
|
||||
// 示例 1:
|
||||
// 输入: nums = [2,3,-2,4]
|
||||
// 输出: `6`
|
||||
// 解释: 子数组 [2,3] 有最大乘积 6。
|
||||
// 示例 2:
|
||||
// 输入: nums = [-2,0,-1]
|
||||
// 输出: 0
|
||||
// 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
|
||||
//
|
||||
// 提示:
|
||||
// - `1 <= nums.length <= 2 * 10^4`
|
||||
// - `-10 <= nums[i] <= 10`
|
||||
// - `nums` 的任何子数组的乘积都 保证 是一个 32-位 整数
|
||||
//
|
||||
func maxProduct(nums []int) int {
|
||||
|
||||
}
|
||||
|
||||
func TestMaxProduct(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
nums []int
|
||||
want int
|
||||
}{
|
||||
{"example1", []int{2, 3, -2, 4}, 6},
|
||||
{"example2", []int{-2, 0, -1}, 0},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
got := maxProduct(c.nums)
|
||||
if got != c.want {
|
||||
t.Errorf("got %v, want %v", got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user