Gidhub BE Developer

[Programmers] 12917. 문자열 내림차순으로 배치하기

2018-07-22
goodGid

Problem

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


[1] Answer Code (18. 07. 22)

#include <functional>
string solution(string s) {
    string answer = "";
    vector<char> lower,upper;
    int size = (int)s.length();
    for(int i=0; i<size; i++){
        if (s[i] >= 97 && s[i] <= 122) { // Lower Case
            lower.push_back(s[i]);
        }
        if (s[i] >= 65 && s[i] <= 90) { // Upper Case
            upper.push_back(s[i]);
        }
    }
    sort(lower.begin(), lower.end(), greater<int>());
    sort(upper.begin(), upper.end(), greater<int>());
    
    for(int i=0; i < lower.size(); i++){
        answer += lower[i];
    }
    for(int i=0; i < upper.size(); i++){
        answer += upper[i];
    }
   
    return answer;
}

Review

  • to_string()를 사용하여 int를 string으로 Fromatting !

[2] Answer Code (18. 07. 22)


string solution(string s) {
    // [1]
    #include <functional>
    sort (s.begin(), s.end(), greater<char>());

    // [2]
    sort(s.rbegin(),s.rend());

    return s;
}

Review

  • s.rbegin() / s.rend()는 내림차순을 해준다.

[3] Answer Code (18. 09. 12)

string solution(string s) {
    vector<char> v1,v2;
    int size = (int) s.size();
    for(int i=0; i<size; i++){
        if( s[i] >= 97 && s[i] <= 122){ // lower
            v1.push_back(s[i]);
        }
        else{ // Upper
            v2.push_back(s[i]);
        }
    }
    
    sort( v1.rbegin(), v1.rend());
    sort( v2.rbegin(), v2.rend());
    
    string answer = "";
    for(int i=0; i<v1.size(); i++)
        answer += v1[i];
    for(int i=0; i<v2.size(); i++)
        answer += v2[i];
    return answer;
}


Comments

Content