zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[LeetCode] Product of Array Except Self

LeetCode of Array product self except
2023-09-14 09:01:04 时间
Given an array of n integers where n 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division

Given an array of n integers where n 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)


两次遍历,第一次遍历,将0到i-1的乘积放入res[i];第二次遍历,记录i+1到len-1的乘积,再与左边的乘积相乘,得到最终结果放入res[i]。


LeetCode 318. Maximum Product of Word Lengths 给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。