티스토리 뷰
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
반응형
'Programmers Solutions > Level-1' 카테고리의 다른 글
[프로그래머스] 서울에서 김서방 찾기 (0) | 2021.02.02 |
---|---|
[프로그래머스] 문자열 다루기 기본 (0) | 2021.02.02 |
[프로그래머스] 문자열 내의 p와 y의 개수 (0) | 2021.02.01 |
[프로그래머스] 문자열 내 마음대로 정렬하기 (0) | 2021.01.31 |
[프로그래머스] 두 정수 사이의 합 (0) | 2021.01.31 |
댓글