代码拉取完成,页面将自动刷新
package com.hit.basmath.interview.top_interview_questions.hard_collection.array_and_strings;
/**
* 238. Product of Array Except Self
* <p>
* Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
* <p>
* Example:
* <p>
* Input: [1,2,3,4]
* Output: [24,12,8,6]
* <p>
* Note: Please solve it without division and in O(n).
* <p>
* Follow up:
* <p>
* Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
*/
public class _238 {
public int[] productExceptSelf(int[] nums) {
// The length of the input array
int length = nums.length;
// Final answer array to be returned
int[] answer = new int[length];
// answer[i] contains the product of all the elements to the left
// Note: for the element at index '0', there are no elements to the left,
// so the answer[0] would be 1
answer[0] = 1;
for (int i = 1; i < length; i++) {
// answer[i - 1] already contains the product of elements to the left of 'i - 1'
// Simply multiplying it with nums[i - 1] would give the product of all
// elements to the left of index 'i'
answer[i] = nums[i - 1] * answer[i - 1];
}
// R contains the product of all the elements to the right
// Note: for the element at index 'length - 1', there are no elements to the right,
// so the R would be 1
int R = 1;
for (int i = length - 1; i >= 0; i--) {
// For the index 'i', R would contain the
// product of all elements to the right. We update R accordingly
answer[i] = answer[i] * R;
R *= nums[i];
}
return answer;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。