쌓고 쌓다
[프로그래머스] 문자열 내림차순으로 배치하기 C++ 풀이 본문
https://school.programmers.co.kr/learn/courses/30/lessons/12917?language=cpp
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이 (1) - 버블 정렬
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
for(int i=0;i<s.length();i++)
{
for(int j=0;j<s.length()-i-1;j++)
{
if(s[j]<s[j+1])
{
char temp = s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
answer=s;
return answer;
}
풀이 (2) - string 정렬 + reverse
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
sort(s.begin(),s.end());
reverse(s.begin(),s.end());
answer=s;
return answer;
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 문자열 내 마음대로 정렬하기 C++ 풀이 (0) | 2022.07.06 |
---|---|
[프로그래머스] 문자열 다루기 기본 C++ 풀이 (0) | 2022.07.06 |
[프로그래머스] 문자열 내 p와 y의 개수 C++ 풀이 (0) | 2022.07.05 |
[프로그래머스] 1차 다트 게임 C++ 풀이 (0) | 2022.07.05 |
[프로그래머스] 두 정수 사이의 합 C++ 풀이 (0) | 2022.07.05 |
Comments