Golang Leetcode 238. Product of Array Except Self.go

时间:2022-06-19
本文章向大家介绍Golang Leetcode 238. Product of Array Except Self.go,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89055013

思路

假如有一个数组为[a,b,c,d],那么在经过这样一轮运算后得到的结果就是[bcd,acd,abd,abc],所以我们可以构造这样两个数组[1,a,ab,abc]和[bcd,cd,d,1].这样两个数组对应项相乘就可以得到结果

code

func productExceptSelf(nums []int) []int {
	ret := make([]int, len(nums))
	for k := range ret {
		ret[k] = 1
	}
	tmp := 1
	for i := 1; i < len(nums); i++ {
		ret[i] = ret[i-1] * nums[i-1]
	}
	for i := len(nums) - 1; i >= 0; i-- {
		ret[i] = ret[i] * tmp
		tmp *= nums[i]
	}
	return ret
}