@Test public void productExceptSelf() { int[] origin = {1, 2, 3, 4}; int[] aim = {24, 12, 8, 6}; origin = ProductArrayExceptSelfSolution.productExceptSelf(origin); for (int i = 0; i < origin.length; i++) { assertEquals(aim[i], origin[i]); } }
static int[] productExceptSelf(int[] nums) { int len = nums.length; int[] result = new int[len]; result[0] = 1; for (int i = 1; i < len; i++) { result[i] = result[i - 1] * nums[i - 1]; } int right = 1; for (int i = len - 1; i >= 0; i--) { result[i] *= right; right *= nums[i]; } return result; }
ProductArrayExceptSelfSolution { static int[] productExceptSelf(int[] nums) { int len = nums.length; int[] result = new int[len]; result[0] = 1; for (int i = 1; i < len; i++) { result[i] = result[i - 1] * nums[i - 1]; } int right = 1; for (int i = len - 1; i >= 0; i--) { result[i] *= right; right *= nums[i]; } return result; } }
ProductArrayExceptSelfSolution { static int[] productExceptSelf(int[] nums) { int len = nums.length; int[] result = new int[len]; result[0] = 1; for (int i = 1; i < len; i++) { result[i] = result[i - 1] * nums[i - 1]; } int right = 1; for (int i = len - 1; i >= 0; i--) { result[i] *= right; right *= nums[i]; } return result; }  }
ProductArrayExceptSelfSolution { static int[] productExceptSelf(int[] nums) { int len = nums.length; int[] result = new int[len]; result[0] = 1; for (int i = 1; i < len; i++) { result[i] = result[i - 1] * nums[i - 1]; } int right = 1; for (int i = len - 1; i >= 0; i--) { result[i] *= right; right *= nums[i]; } return result; }   }
ProductArrayExceptSelfSolution { static int[] productExceptSelf(int[] nums) { int len = nums.length; int[] result = new int[len]; result[0] = 1; for (int i = 1; i < len; i++) { result[i] = result[i - 1] * nums[i - 1]; } int right = 1; for (int i = len - 1; i >= 0; i--) { result[i] *= right; right *= nums[i]; } return result; }    }