File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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+
You canโt perform that action at this time.
0 commit comments