LeetCode: Product of Array Except Self

Dynamic Programming

 1 public class Solution {
 2     public int[] productExceptSelf(int[] nums) {
 3         int[] ans = new int[nums.length];
 4         for (int i = 0; i < ans.length; i++) ans[i] = 1;
 5         int product = 1;
 6         for (int i = 1; i < ans.length; i++) {
 7             product *= nums[i-1];
 8             ans[i] *= product;
 9         }
10         product = 1;
11         for (int i = ans.length-2; i >= 0; i--) {
12             product *= nums[i+1];
13             ans[i] *= product;
14         }
15         return ans;
16     }
17 }
原文地址:https://www.cnblogs.com/yingzhongwen/p/6098225.html