comments | difficulty | edit_url |
---|---|---|
true |
简单 |
给定一个数组 A[0,1,…,n-1]
,请构建一个数组 B[0,1,…,n-1]
,其中 B[i]
的值是数组 A
中除了下标 i
以外的元素的积, 即 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]
。不能使用除法。
示例:
输入: [1,2,3,4,5] 输出: [120,60,40,30,24]
提示:
- 所有元素乘积之和不会溢出 32 位整数
a.length <= 100000
我们先创建一个长度为
接下来,我们从左到右遍历数组
然后,我们从右到左遍历数组
最终,数组
时间复杂度
class Solution:
def constructArr(self, a: List[int]) -> List[int]:
n = len(a)
ans = [0] * n
left = right = 1
for i in range(n):
ans[i] = left
left *= a[i]
for i in range(n - 1, -1, -1):
ans[i] *= right
right *= a[i]
return ans
class Solution {
public int[] constructArr(int[] a) {
int n = a.length;
int[] ans = new int[n];
for (int i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= a[i];
}
for (int i = n - 1, right = 1; i >= 0; --i) {
ans[i] *= right;
right *= a[i];
}
return ans;
}
}
class Solution {
public:
vector<int> constructArr(vector<int>& a) {
int n = a.size();
vector<int> ans(n);
for (int i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= a[i];
}
for (int i = n - 1, right = 1; ~i; --i) {
ans[i] *= right;
right *= a[i];
}
return ans;
}
};
func constructArr(a []int) []int {
n := len(a)
ans := make([]int, n)
for i, left := 0, 1; i < n; i++ {
ans[i] = left
left *= a[i]
}
for i, right := n-1, 1; i >= 0; i-- {
ans[i] *= right
right *= a[i]
}
return ans
}
function constructArr(a: number[]): number[] {
const n = a.length;
const ans: number[] = Array(n);
for (let i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= a[i];
}
for (let i = n - 1, right = 1; i >= 0; --i) {
ans[i] *= right;
right *= a[i];
}
return ans;
}
/**
* @param {number[]} a
* @return {number[]}
*/
var constructArr = function (a) {
const n = a.length;
const ans = new Array(n);
for (let i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= a[i];
}
for (let i = n - 1, right = 1; ~i; --i) {
ans[i] *= right;
right *= a[i];
}
return ans;
};
public class Solution {
public int[] ConstructArr(int[] a) {
int n = a.Length;
int[] ans = new int[n];
int left = 1, right = 1;
for (int i = 0; i < n; i++) {
ans[i] = left;
left *= a[i];
}
for (int i = n - 1; i > -1; i--) {
ans[i] *= right;
right *= a[i];
}
return ans;
}
}
class Solution {
func constructArr(_ a: [Int]) -> [Int] {
let n = a.count
guard n > 0 else { return [] }
var ans = [Int](repeating: 1, count: n)
var left = 1
for i in 0..<n {
ans[i] = left
left *= a[i]
}
var right = 1
for i in (0..<n).reversed() {
ans[i] *= right
right *= a[i]
}
return ans
}
}