티스토리 뷰


title: "문자열 내림차순으로 배치하기"
category: 프로그래머스[Level-1]
tags: [C++, JavaScript, 프로그래머스]
date: "2021-01-19"


문제 링크

문자열 내림차순으로 배치하기

C++

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string s) {
    string answer(s);

    sort(answer.rbegin(), answer.rend());   // 내림차순 정렬

    return answer;
}

JavaScript

function solution(s) {
  var answer = "";

  answer = s
    .split("") // 배열로 변환
    .sort((a, b) => {
      // 사전 반대 정렬
      if (a > b) return -1;
      else return 1;
    })
    .join(""); // string으로 변환

  console.log(answer);

  return answer;
}
728x90
반응형
댓글
02-12 10:14
링크