标签:ane des 计算 btn button lin group 返回 pre
给定一个整数数组A。
定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法。
给出A=[1, 2, 3],返回 B为[6, 3, 2]
代码
class Solution { public: /* * @param nums: Given an integers array A * @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1] */ vector<long long> productExcludeItself(vector<int> &nums) { // write your code here int len = nums.size(); vector<long long> left(len, 1); vector<long long> right(len, 1); vector<long long> res(len, 1); for (int i = 1; i < len; ++i) { left[i] = left[i - 1] * nums[i - 1]; } for (int i = len - 2; i >= 0; --i) { right[i] = right[i + 1] * nums[i + 1]; } for(int i = 0; i < len; ++i) { res[i] = left[i] * right[i]; } return res; } };
标签:ane des 计算 btn button lin group 返回 pre
原文地址:http://www.cnblogs.com/gousheng/p/7642878.html