2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_238.java 1.85 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
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;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助