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

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

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ

728x90

์ฒ˜์Œ์—” ๋ชจ๋“  ์ˆ˜๋ฅผ ๋”ํ•œ๊ฑธ Set ์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์ค‘๋ณต๋˜๋Š” ์ˆ˜๋ฅผ ์—†์•จ๊นŒ ํ•˜๋‹ค๊ฐ€, 

๊ทธ๋Ÿผ ๊ฐ™์€ ๊ฐ™์€ 1, 3 ์„ ๋”ํ•œ ๊ฒƒ๊ณผ 3, 1 ์„ ๋”ํ•˜๋ฉด ๊ฐ™์€๋ฐ ๊ตณ์ด ํ•ด์•ผํ•˜๋‚˜ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

function solution(numbers) {
    let sums = [];
    numbers.map((v, i) =>
        numbers.map((v1, i1) =>
            i !== i1 && !sums.includes(v + v1) && sums.push(v + v1)))
   return sums.sort((a, b)=> a - b);
}

 

์–ด์จŒ๋“  ๋ฐ˜๋ณต๋ฌธ์„ ๋‘๋ฒˆ ๋Œ์•„์•ผํ•˜๋Š”๋ฐ, ๊ทธ๋Ÿผ ์˜ˆ์™ธ๋ฅผ ๋‘ฌ์„œ ์ตœ๋Œ€ํ•œ ๋œ ์—ฐ์‚ฐํ•˜๋„๋ก ํ•ด๋ณด์ž..! 

๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

 

๊ทผ๋ฐ ์ง€๊ธˆ ์ž‘์„ฑํ•˜๋ฉด์„œ ๋‹ค์‹œ ๋ณด๋‹ˆ, ์–ด์ฐจํ”ผ includes ํ•  ๋•Œ ์ „๋ถ€ ๊ณ„์‚ฐํ•˜๊ธด ํ•˜๋„ค.... ๋จธ์“ฑ~

 

 

 

function solution(numbers) {
    const temp = []

    for (let i = 0; i < numbers.length; i++) {
        for (let j = i + 1; j < numbers.length; j++) {
            temp.push(numbers[i] + numbers[j])
        }
    }

    const answer = [...new Set(temp)]

    return answer.sort((a, b) => a - b)
}

 

์ด๋ ‡๊ฒŒ ํ•˜๋Š”๊ฒŒ ์†๋„๋„ ํ›จ์”ฌ ๋น ๋ฅด๋‹ค....!!

๋‹ค์Œ์—” set ์‚ฌ์šฉํ•ด์•ผ์ง€~

728x90
๋ฐ˜์‘ํ˜•