๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

Programmers Lv.1 : ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

by Youcodein 2022. 11. 7.
728x90
๋ฐ˜์‘ํ˜•

Programmers Lv.1 : ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

" ํ•จ์ˆ˜ solution์€ ์ •์ˆ˜ n์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด n์ด 118372๋ฉด 873211์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. "

n์€ 1์ด์ƒ 8000000000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‚ด ํ’€์ด

function solution(n) {
  return String(n).split("").sort((a, b) => b - a).join("")/1
}

์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ˆ˜ ๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์ €๋„ ์ด์ „ ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด์— ๋‚˜๋ˆ„๊ธฐ๋ฅผ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

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

function solution(n) {
  var answer = "";
  n = n + "";
  var emptyArray = [];
  for (var i = 0; i < n.length; i++) {
      emptyArray.push(n[i]);
  }
  for (var j = 0; j < emptyArray.length; j++) {
      if (emptyArray[j] < emptyArray[j+1]) {
          var temp = emptyArray[j];
          emptyArray[j] = emptyArray[j+1];
          emptyArray[j+1] = temp;
          j = -1;
      }
  }
  for (var k = 0; k < emptyArray.length; k++) {
      answer += emptyArray[k];
  }
  answer = Number(answer);
  return answer;
}
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€