본문 바로가기
Computer Science/Coding Test

c++(cpp) 프로그래머스 - 코딩 테스트 : 문자열 내림차순으로 배치하기

by hzyiunn 2022. 8. 11.
728x90
반응형

나는 쉽게 algorithm include하고 역으로 sort하는 함수 사용했는데

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

string solution(string s) {
    string answer = "";
    sort(s.begin(), s.end(), greater<char>());
    answer = s;
    return answer;
}

아래 코드처럼 rbegin(), rend()를 사용할 수도 있다는 걸 몰랐다.. 새로 알아가는 중

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

string solution(string s) {
    string answer = "";
    sort(s.rbegin(),s.rend());

    return s;
}
728x90
반응형

댓글