1. 문제 및 예시
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].
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.
2. 풀이
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
let result = [];
let lp = 1, rp = 1;
for(let i = 0; i < nums.length; i ++){
result.push(lp);
lp *= nums[i];
}
for(let i = nums.length -1; i >= 0; i --){
result[i] *= rp;
rp *= nums[i];
}
return result;
}
** 정방향으로 한번, 역방향으로 한번.
3. 결과
18 / 18 test cases passed.
Status: Accepted
Runtime: 80 ms
Memory Usage: 42.2 MB
반응형
'Programming' 카테고리의 다른 글
[LDAP] LDAP Account Manager 사용하기 (0) | 2020.04.28 |
---|---|
[JIRA] Ubuntu Server에 JIRA 설치하기 (0) | 2020.04.28 |
[LeetCode] [Medium] [JS] 525. Contiguous Array (0) | 2020.04.14 |
[LeetCode] [Easy] [JS] 1046. Last Stone Weight (0) | 2020.04.12 |
[LeetCode] [Easy] [JS] 543. Diameter of Binary Tree (0) | 2020.04.12 |