Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฐ€์žฅ ํฐ ์ˆ˜ (js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฐ€์žฅ ํฐ ์ˆ˜ (js)

Lennon 2021. 10. 25. 22:08
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/42746?language=javascript# 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฐ€์žฅ ํฐ ์ˆ˜

0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜๋ฅผ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด ์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์ •์ˆ˜๊ฐ€ [6, 10, 2]๋ผ๋ฉด [6102, 6210, 1062, 1026, 2610, 2106]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ด์ค‘ ๊ฐ€์žฅ ํฐ

programmers.co.kr

 

function solution(numbers) {
    
    numbers = numbers.map((v) => v+'');
    numbers.sort((a,b) => a+b > b+a ? -1 : 1);
    
    while(numbers[0] === "0"){
        numbers.shift();
    }
    
    return numbers.length !== 0 ? numbers.join("") : '0';
}

 

์ด ์ „์—” ์ž…๋ ฅ๊ฐ’์ด 0์ธ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์—†์—ˆ๋Š”๋ฐ, ์ถ”๊ฐ€๊ฐ€ ๋ผ ํ‰์†Œ์ฒ˜๋Ÿผ ํ’€๋ฉด ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 11๋ฒˆ์ด ๋ง‰ํžŒ๋‹ค.

 

11๋ฒˆ์˜ ๋ฐ˜๋ก€๋Š” ์ž…๋ ฅ๊ฐ’ [0,0,0] ๊ฒฐ๊ณผ๊ฐ’ "0" ์ด๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments