λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
"곡뢀" π‘Ÿπ‘’π‘π‘œπ‘Ÿπ‘‘/π΄π‘™π‘”π‘œπ‘Ÿπ‘–π‘‘β„Žπ‘š

[JS/ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] / [μ›”κ°„ μ½”λ“œ μ±Œλ¦°μ§€ μ‹œμ¦Œ 1] 두 개 λ½‘μ•„μ„œ λ”ν•˜κΈ°

by ΰ·† Yoni ΰ·† 2022. 2. 16.
728x90

[μ›”κ°„ μ½”λ“œ μ±Œλ¦°μ§€ μ‹œμ¦Œ 1]

두 개 λ½‘μ•„μ„œ λ”ν•˜κΈ°

 

 

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - 두 개 λ½‘μ•„μ„œ λ”ν•˜κΈ°

μ •μˆ˜ λ°°μ—΄ numbersκ°€ μ£Όμ–΄μ§‘λ‹ˆλ‹€. numbersμ—μ„œ μ„œλ‘œ λ‹€λ₯Έ μΈλ±μŠ€μ— μžˆλŠ” 두 개의 수λ₯Ό 뽑아 λ”ν•΄μ„œ λ§Œλ“€ 수 μžˆλŠ” λͺ¨λ“  수λ₯Ό 배열에 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ λ‹΄μ•„ return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•΄μ£Όμ„Έμš”. μ œν•œ

programmers.co.kr

 

My μ½”λ“œ

function solution(numbers) {
    var answer = [];
    for(let i=0;i<numbers.length;i++) {
        for(let j=i+1;j<numbers.length;j++) {
            let sum = numbers[i] + numbers[j];
            if(answer.includes(sum) == false) {
                answer.push(sum);
            }
        }
    }
    answer.sort((a, b) => a - b);

    return answer;
}

 

λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이

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)
}
728x90

λŒ“κΈ€