Written by
java-style
on
on
[leetcode] 배열 - 238번 Product Of ArrayExceptSelf (Java)
[leetcode] 배열 - 238번 Product Of ArrayExceptSelf (Java)
접근한 방법
배열
풀이방법
처음에 이중 반복문으로 풀어서 시간초과가 나왔다.
따라서, 블로그를 참조하여 이해할 수 있었다.
소스코드
package leetcode.array; import java.util.EnumSet; public class Q238_ProductOfArrayExceptSelf { public static int[] productExceptSelf(int[] nums) { int[] result = new int[nums.length]; // result를 1로 초기화 for(int i=0 ; i= 0; i--) { nums[i] = nums[i] * nums[i + 1]; } // 앞에서 구한 result와 nums를 곱해준다. (마지막 인덱스는 생략) for (int i = 0; i < nums.length - 1; i++) { result[i] = result[i] * nums[i + 1]; } return result; } public static void main(String[] args) { int[] nums = {1,2,3,4}; int[] result = productExceptSelf(nums); for (int i = 0; i
from http://doongjeol.tistory.com/129 by ccl(A) rewrite - 2021-10-30 20:27:56