프로그래머스 - 문자열 내림차순으로 배치하기

well-life-gm·2021년 11월 6일
0

프로그래머스

목록 보기
36/125

프로그래머스 - 문자열 내림차순으로 배치하기

compare 함수를 써서 앞부터 내림차순 정렬을 해도 되고,
rbegin(), rend()를 해서 뒤에서부터 오름차순 정렬을 해도 된다.

// Compare 함수
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
bool compare(const char &a, const char &b)
{
    return a > b;
}
string solution(string s) {
    sort(s.begin(), s.end(), compare);
    string answer = s;
    return answer;
}
// rbegin(), rend()
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string s) {
    sort(s.rbegin(), s.rend());
    string answer = s;
    return answer;
}
profile
내가 보려고 만든 블로그

0개의 댓글