๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค1๋‹จ๊ณ„ : ํ–‰๋ ฌ์˜ ๋ง์…ˆ

by _ํ† ๋งคํ†  2022. 11. 25.
728x90

๐Ÿ–ฑ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 1๋‹จ๊ณ„ : ํ–‰๋ ฌ์˜ ๋ง์…ˆ ๐Ÿ–ฑ


๐Ÿ“– ๋ฌธ์ œ ์„ค๋ช… ๐Ÿ“–

ํ–‰๋ ฌ์˜ ๋ง์…ˆ์€ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋‘ ํ–‰๋ ฌ์˜ ๊ฐ™์€ ํ–‰, ๊ฐ™์€ ์—ด์˜ ๊ฐ’์„ ์„œ๋กœ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2๊ฐœ์˜ ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ํ–‰๋ ฌ ๋ง์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

๐Ÿงธ ์ œํ•œ ์‚ฌํ•ญ ๐Ÿงธ

ํ–‰๋ ฌ arr1, arr2์˜ ํ–‰๊ณผ ์—ด์˜ ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

function solution(arr1, arr2) {
    let answer = [];
    
    for(i=0; i<arr1.length; i++){
        let num = [];
        for(j=0; j<arr1[i].length; j++){
            num.push(arr1[i][j] + arr2[i][j])
        }
        answer.push(num)
    }
    return answer;
}
// push()๋Š” ๋ฐฐ์—ด์˜ ๋์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋ฐฐ์—ด์˜ ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
// arr1 == [[1,2],[2,3]] arr2 == [[3,4],[5,6]]์ผ ๋•Œ [[4,6],[7,9]]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
// arr1 == [[1],[2]] arr2 == [[3],[4]]์ผ ๋•Œ [[4],[6]]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿงธ ๋ฌธ์ œ ํ’€์ด ๐Ÿงธ

1. ๋จผ์ € ๋ณ€์ˆ˜์— ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.
2. ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด arr1์˜ ๊ธธ์ด ๋งŒํผ ๋ฐ˜๋ณตํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ ๋’ค ๋ฐฐ์—ด์•ˆ์— ๋˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•ด ๋นˆ ๋ฐฐ์—ด์„ ์ €์žฅํ•œ ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
3. num์•ˆ์— push()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ arr1์˜ i๋ฒˆ์งธ ๋ฐฐ์—ด์•ˆ์˜ j๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ฐ’ + arr2์˜ i๋ฒˆ์งธ ๋ฐฐ์—ด์•ˆ์˜ j๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋„ฃ์–ด ์ค๋‹ˆ๋‹ค.
4. ๊ทธ ๋’ค num์— ์ €์žฅ๋œ 2๊ฐœ์˜ ๋ฐฐ์—ด์„ ์ฒ˜์Œ์— ๋งŒ๋“ค์–ด ๋‘” ๋นˆ ๋ฐฐ์—ด์— pushํ•˜์—ฌ ๋‘๊ฐœ์˜ ๋ฐฐ์—ด์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.


๐Ÿงธ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ๐Ÿงธ

function sumMatrix(A,B){
    return A.map((a,i) => a.map((b, j) => b + B[i][j]));
}

map ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•˜์˜€๋„ค์š”... map์„ ์ž˜ ํ™œ์šฉํ•˜์ง€ ์•Š์•„์„œ ์กฐ๊ธˆ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๊ธฐ๋„ ํ•˜๋„ค์šฉ..ใ…œ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css