-
BingoPython:
class Solution: # @param {integer[]} nums # @return {integer[]} def productExceptSelf(self, nums): size = len(nums) output = [1] * size left = 1 for x in range(size - 1): left *= nums[x] output[x + 1] *= left right = 1 for x in range(size - 1, 0, -1): right *= nums[x] output[x - 1] *= right return output
-
BingoC++解法一:
class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int n = nums.size(); vector<int> fwd(n, 1), bwd(n, 1), res(n); for (int i = 0; i < n - 1; ++i) { fwd[i + 1] = fwd[i] * nums[i]; } for (int i = n - 1; i > 0; --i) { bwd[i - 1] = bwd[i] * nums[i]; } for (int i = 0; i < n; ++i) { res[i] = fwd[i] * bwd[i]; } return res; } };
-
BingoC++解法二:
class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> res(nums.size(), 1); for (int i = 1; i < nums.size(); ++i) { res[i] = res[i - 1] * nums[i - 1]; } int right = 1; for (int i = nums.size() - 1; i >= 0; --i) { res[i] *= right; right *= nums[i]; } return res; } };
-
BingoJava解法一:
public class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] res = new int[n]; int[] fwd = new int[n], bwd = new int[n]; fwd[0] = 1; bwd[n - 1] = 1; for (int i = 1; i < n; ++i) { fwd[i] = fwd[i - 1] * nums[i - 1]; } for (int i = n - 2; i >= 0; --i) { bwd[i] = bwd[i + 1] * nums[i + 1]; } for (int i = 0; i < n; ++i) { res[i] = fwd[i] * bwd[i]; } return res; } }
-
BingoJava解法二:
public class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length, right = 1; int[] res = new int[n]; res[0] = 1; for (int i = 1; i < n; ++i) { res[i] = res[i - 1] * nums[i - 1]; } for (int i = n - 1; i >= 0; --i) { res[i] *= right; right *= nums[i]; } return res; } }
-