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

김현민·2021년 3월 16일
0

Algorithm

목록 보기
38/126
post-thumbnail

문제

내 코드


#include <bits/stdc++.h>
using namespace std;

string solution(string s)
{
    vector<int> v;
    string answer = "";
    int length = s.size();

    for (int i = 0; i < length; i++)
    {
        v.push_back(s[i]);
    }

    sort(v.begin(), v.end(), greater<int>());
    for (int i = 0; i < length; i++)
    {
        answer += v[i];
    }

    return answer;
}
  • 문자열 하나하나를 받을 벡터를 만든다. (int형 이니까 ascii코드로 받게 된다)
  • 내림차순으로 정렬하는 코드
    - sort(v.begin(), v.end(), greater<int>());



다른사람의 코드


#include <bits/stdc++.h>
using namespace std;

string solution(string s)
{

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

    return answer;
}
  • char 자료형을 이용해 한글자씩 받는다.
  • string 자료형도 반복자를 통해 정렬할 수 있다.
profile
Jr. FE Dev

0개의 댓글