|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=238 lang=java |
| 3 | + * |
| 4 | + * [238] Product of Array Except Self |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/product-of-array-except-self/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (61.42%) |
| 10 | + * Total Accepted: 716.2K |
| 11 | + * Total Submissions: 1.2M |
| 12 | + * Testcase Example: '[1,2,3,4]' |
| 13 | + * |
| 14 | + * Given an array nums of n integers where n > 1, return an array output such |
| 15 | + * that output[i] is equal to the product of all the elements of nums except |
| 16 | + * nums[i]. |
| 17 | + * |
| 18 | + * Example: |
| 19 | + * |
| 20 | + * |
| 21 | + * Input: [1,2,3,4] |
| 22 | + * Output: [24,12,8,6] |
| 23 | + * |
| 24 | + * |
| 25 | + * Constraint: It's guaranteed that the product of the elements of any prefix |
| 26 | + * or suffix of the array (including the whole array) fits in a 32 bit |
| 27 | + * integer. |
| 28 | + * |
| 29 | + * Note: Please solve it without division and in O(n). |
| 30 | + * |
| 31 | + * Follow up: |
| 32 | + * Could you solve it with constant space complexity? (The output array does |
| 33 | + * not count as extra space for the purpose of space complexity analysis.) |
| 34 | + * |
| 35 | + */ |
| 36 | +// class Solution { |
| 37 | +// public int[] productExceptSelf(int[] nums) { |
| 38 | +// |
| 39 | +// int[] res = new int[nums.length]; |
| 40 | +// |
| 41 | +// for (int i = 0; i < nums.length; ++i) { |
| 42 | +// if (res[i] == 0) { res[i] = 1; } |
| 43 | +// for (int j = 0; j < nums.length; ++j) { |
| 44 | +// if (i == j) { continue; } |
| 45 | +// res[i] *= nums[j]; |
| 46 | +// } |
| 47 | +// } |
| 48 | +// |
| 49 | +// return res; |
| 50 | +// } |
| 51 | +// } |
| 52 | + |
| 53 | +class Solution { |
| 54 | + public int[] productExceptSelf(int[] nums) { |
| 55 | + |
| 56 | + // The length of the input array |
| 57 | + int length = nums.length; |
| 58 | + |
| 59 | + // The left and right arrays as described in the algorithm |
| 60 | + int[] L = new int[length]; |
| 61 | + int[] R = new int[length]; |
| 62 | + |
| 63 | + // Final answer array to be returned |
| 64 | + int[] answer = new int[length]; |
| 65 | + |
| 66 | + // L[i] contains the product of all the elements to the left |
| 67 | + // Note: for the element at index '0', there are no elements to the left, |
| 68 | + // so L[0] would be 1 |
| 69 | + L[0] = 1; |
| 70 | + for (int i = 1; i < length; i++) { |
| 71 | + |
| 72 | + // L[i - 1] already contains the product of elements to the left of 'i - 1' |
| 73 | + // Simply multiplying it with nums[i - 1] would give the product of all |
| 74 | + // elements to the left of index 'i' |
| 75 | + L[i] = nums[i - 1] * L[i - 1]; |
| 76 | + } |
| 77 | + |
| 78 | + // R[i] contains the product of all the elements to the right |
| 79 | + // Note: for the element at index 'length - 1', there are no elements to the right, |
| 80 | + // so the R[length - 1] would be 1 |
| 81 | + R[length - 1] = 1; |
| 82 | + for (int i = length - 2; i >= 0; i--) { |
| 83 | + |
| 84 | + // R[i + 1] already contains the product of elements to the right of 'i + 1' |
| 85 | + // Simply multiplying it with nums[i + 1] would give the product of all |
| 86 | + // elements to the right of index 'i' |
| 87 | + R[i] = nums[i + 1] * R[i + 1]; |
| 88 | + } |
| 89 | + |
| 90 | + // Constructing the answer array |
| 91 | + for (int i = 0; i < length; i++) { |
| 92 | + // For the first element, R[i] would be product except self |
| 93 | + // For the last element of the array, product except self would be L[i] |
| 94 | + // Else, multiple product of all elements to the left and to the right |
| 95 | + answer[i] = L[i] * R[i]; |
| 96 | + } |
| 97 | + |
| 98 | + return answer; |
| 99 | + } |
| 100 | +} |
0 commit comments