You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
614 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class33;
public class Problem_0238_ProductOfArrayExceptSelf {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
ans[0] = nums[0];
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] * nums[i];
}
int right = 1;
for (int i = n - 1; i > 0; i--) {
ans[i] = ans[i - 1] * right;
right *= nums[i];
}
ans[0] = right;
return ans;
}
// 扩展 : 如果仅仅是不能用除号把结果直接填在nums里呢
// 解法数一共几个0每一个位得到结果就是a / b位运算替代 /,之前的课讲过(新手班)
}