알고리즘/프로그래머스
[C++] 중복된 문자 제거, 가장 큰 수 찾기
parkkingcar
2023. 5. 17. 20:19
중복된 문자 제거
문자열이 매개변수로 주어질 때, 문자열에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열 return
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string my_string) {
string answer = "";
vector <string> str;
for(int i=0; i < my_string.size(); i++){
if(find(str.begin(), str.end(), string(1, my_string[i])) == str.end()){
answer += string(1, my_string[i]);
}
str.push_back(string(1, my_string[i]));
}
return answer;
}
가장 큰 수 찾기
정수 배열이 매개변수로 주어질 때, 가장 큰 수와 그 수의 인덱스를 담은 배열 return
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array) {
vector<int> answer;
int max, max_idx;
max = *max_element(array.begin(), array.end());
max_idx = max_element(array.begin(), array.end()) - array.begin();
answer.push_back(max);
answer.push_back(max_idx);
return answer;
}