프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이과정
문자열도 char형이 여러개 이어진 것이기 때문에 sort를 할 수 있다.
rbegin()과 rend()는 내림차순을 한다는 것이다.
풀이1
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
sort(s.rbegin(), s.rend());
answer = s;
return answer;
}
풀이2
#include <string>
#include <vector>
using namespace std;
string solution(string s) {
string answer = "";
// sort 내림차순 구현
for(int i = 0; i < s.size(); i++){
for(int j = 0; j < s.size(); j++){
if(s[i] > s[j]){
int temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
answer = s;
return answer;
}
'🍞 Problem Solving > Programmers' 카테고리의 다른 글
[프로그래머스][Level1] 부족한 금액 계산하기 c++ (0) | 2022.09.02 |
---|---|
[프로그래머스][Level1] 시저 암호 c++ (0) | 2022.09.02 |
[프로그래머스][Level1] 문자열 내 p와 y의 개수 c++ (0) | 2022.09.02 |
[프로그래머스][Level1] 두 정수 사이의 합 c++ (0) | 2022.09.02 |
[프로그래머스][Level1] 두 개 뽑아서 더하기 c++ (0) | 2022.09.02 |