Skip to content

Commit e25830b

Browse files
committed
add product of array excpet self
1 parent f502b24 commit e25830b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
1. ๋ฌธ์ œ ์ดํ•ด
3+
answer[i] ์—๋Š” nums[i]๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆ˜๋“ค์„ ๋ชจ๋‘ ๊ณฑ์…ˆ ํ–ˆ์„๋•Œ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ
4+
5+
2. naive algorithm๋„์ถœ
6+
7+
๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ answer[i]์— ๊ฐ’์„ ๋„ฃ์„๋•Œ nums๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•ด์„œ nums[i]๋ฅผ ์ œ์™ธํ•œ ์ˆ˜๋ฅผ ๊ณฑ์…ˆํ•ด์„œ ๋„ฃ์œผ๋ฉด ๋์ด๋‹ค.
8+
ํ•˜์ง€๋งŒ, nums์˜ ๊ธธ์ด๊ฐ€ ๊ธธ๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(N^2)์ด๋‹ค.
9+
10+
answer[i]์—๋Š” nums[i]์˜ ์™ผ์ชผ๊นŒ์ง€์˜ ๊ณฑ๊ณผ ์˜ค๋ฅธ์ชฝ ๊นŒ์ง€์˜ ๊ณฑ์„ ๊ณฑํ•˜๋ฉด ๋์ด๋ฏ€๋กœ
11+
answer[i]์— nums[i]์˜ ์™ผ์ชฝ๊นŒ์ง€ ํ•ฉ์„ ๋„ฃ์–ด๋†“๊ณ  answer[i]์— nums[i]์˜ ์˜ค๋ฅธ์ชฝ๊ฐ€์ง€์˜ ๊ณฑ์„ ๊ณฑํ•œ๋‹ค.
12+
13+
3. ์‹œ๊ฐ„๋ณต์žก๋„ ๋ถ„์„
14+
15+
4. ์ฝ”๋“œ๊ตฌํ˜„
16+
*/
17+
class sangyyypark {
18+
public int[] productExceptSelf(int[] nums) {
19+
int [] answer = new int[nums.length];
20+
21+
int left = 1;
22+
for(int i = 0; i < nums.length; i++) {
23+
answer[i] = left;
24+
left = nums[i] * left;
25+
}
26+
27+
int right = 1;
28+
for(int i = nums.length - 1; i >= 0; i--) {
29+
answer[i] = answer[i] * right;
30+
right = nums[i] * right;
31+
}
32+
return answer;
33+
}
34+
35+
}
36+

0 commit comments

Comments
ย (0)