-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathL0238_ProductOfArrayExceptSelf.java
74 lines (67 loc) · 2.46 KB
/
L0238_ProductOfArrayExceptSelf.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* https://leetcode.cn/problems/product-of-array-except-self/
*
* 给你一个整数数组 nums,返回 数组 answer,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。
*
* 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。
*
* 请不要使用除法,且在 O(n) 时间复杂度内完成此题。
*
* 示例 1:
* 输入: nums = [1,2,3,4]
* 输出: [24,12,8,6]
*
* 示例 2:
* 输入: nums = [-1,1,0,-3,3]
* 输出: [0,0,9,0,0]
*
* 提示:
* 2 <= nums.length <= 10⁵
* -30 <= nums[i] <= 30
* 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内
*
* 进阶:你可以在 O(1) 的额外空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)
*/
public class L0238_ProductOfArrayExceptSelf {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] answer = new int[n];
// 计算每个位置左边所有数的乘积
answer[0] = 1;
for (int i = 1; i < n; i++) {
answer[i] = answer[i - 1] * nums[i - 1];
}
// 从右到左遍历,计算右边所有数的乘积,并与左边的乘积相乘
int rightProduct = 1;
for (int i = n - 1; i >= 0; i--) {
answer[i] *= rightProduct;
rightProduct *= nums[i];
}
return answer;
}
public static void main(String[] args) {
L0238_ProductOfArrayExceptSelf solution = new L0238_ProductOfArrayExceptSelf();
// 测试用例 1
int[] nums1 = {1, 2, 3, 4};
int[] result1 = solution.productExceptSelf(nums1);
System.out.print("Test Case 1: [");
for (int i = 0; i < result1.length; i++) {
System.out.print(result1[i]);
if (i < result1.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
// 测试用例 2
int[] nums2 = {-1, 1, 0, -3, 3};
int[] result2 = solution.productExceptSelf(nums2);
System.out.print("Test Case 2: [");
for (int i = 0; i < result2.length; i++) {
System.out.print(result2[i]);
if (i < result2.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
}