๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆ˜์—ด๊ณผ ๊ตฌ๊ฐ„ ์ฟผ๋ฆฌ3

728x90
function solution(arr, queries) {
    let result = [...arr];
    queries.map(indexes => {
        const i1 = indexes[0];
        const i2 = indexes[1];
        result[i1] = arr[i2];
        result[i2] = arr[i1];
        arr = [...result];
    })
    return result;
}

์Œ..

์™œ ๋Š˜ ์•„๋ž˜๊ฐ™์€ ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ์ด ์•ˆ๋‚ ๊นŒ?

function solution(arr, queries) {
    queries.forEach( ([a,b]) => {
        [arr[a],arr[b]] = [arr[b],arr[a]];
    })
    return arr;
}

 

728x90
๋ฐ˜์‘ํ˜•