思路
对于A中第i位,设置两个指针j1 = i - 1,j2 = i + 1. 让两个指针同时往两侧走,直到两侧都到达边界为止,将乘积压入B.
C++ 代码
class Solution {
public:
vector<int> multiply(const vector<int>& A) {
vector<int> B;
for(int i = 0; i < A.size();i++)
{ int j1 = i - 1, j2 = i + 1;
int res = 1;
while(j1 >= 0 || j2 < A.size())
{
if(j1 >= 0) res = res*A[j1];
if(j2 < A.size()) res = res*A[j2];
j1--;
j2++;
}
B.push_back(res);
}
return B;
}
};