대칭되는 문자열 만들기
·
공부/Code Cata
주어진 food 배열을 가지고 규칙에 맞게 왼쪽과 오른쪽이 대칭되는 문자열을 만들어 반환하는 문제였다. *초기 작성 코드#include #include #include using namespace std;string solution(vector food) { string answer = ""; for (int i = 1; i ()); answer += '0' + temp; return answer;}왼쪽을 먼저 규칙에 맞게 만든 후 '0' 과 반대로 정렬한 문자열을 추가해서 완성시키도록 하였다.일단 코드가 길어 가독성 면에서 좋지 않고 2중 for문에 sort까지 사용해서 아쉽다는 생각이 들었다.  *다른 사람 풀이 참고해 수정 ver_1#include #include using n..
람다 함수로 사용자 설정 정의해 정렬하기
·
공부/Code Cata
#include #include #include using namespace std;vector solution(vector strings, int n) { sort(strings.begin(), strings.end(), [n](const string& a, const string& b) { if(a[n] == b[n]) { return a 비교할 인덱스 값 n을 capture해서 람다 함수로 넘겨주어 두 인덱스의 값이 같다면 사전순으로 정렬하고, 같지 않다면 바로 오름차순 정렬하였다. 람다함수[capture](parameters) -> return_type { // function body};capture: 람다 함수 외부의 변수를 가져오는 방식..
문자열에서 영문 숫자 변환하기
·
공부/Code Cata
*unordered_map 사용한 초기 코드더보기#include #include #include using namespace std;string ConvertStirng(const unordered_map NumList, const string& s){ string Result; string Temp; for (char c : s) { // 알파벳 발견 시, 한 글자씩 unordered_map 리스트에 검색하여 Result에 저장 if (isalpha(c)) { Temp += c; auto it = NumList.find(Temp); if (it != NumList.end()) ..
시저 암호(알파벳 순환 이동)
·
공부/Code Cata
#include #include using namespace std;string solution(string s, int n) { for(int i = 0; i = 'a') ? 'a' : 'A'; s[i] = Case + (NowChar - Case + n) % 26; } return s;}s[i] = Case + (NowChar - 'A' + n)  % 26 26개의 알파벳을 'A'를 0부터  'Z'를 25까지의 숫자로 변환한다.만약 NowChar가 'Z'라고 가정하면 'Z' - 'A'의 값은 25가 된다.여기서 n의 값이 3이라고 가정한다면 28 % 26의 계산식이 되고최종적으로 'A' + 2의 값인 'C'가 s[i]에 들어가게 된다.
최소 직사각형 구하기
·
공부/Code Cata
*초기 작성 코드더보기#include #include using namespace std;int solution(vector> sizes) { int Max_W = 0; int Max_H = 0; for(int i = 0; i Max_W) { Max_W = sizes[i][0]; } // 최대 h 값 갱신 if(sizes[i][1] > Max_H) { Max_H = sizes[i][1]; } } return Max_W * Max_H;} #include #include using namespace std;int solution(vector> si..
재귀호출로 배열의 합 검사하기
·
공부/Code Cata
*초기 작성 코드더보기#include #include using namespace std;int solution(vector number) { int answer = 0; int size = number.size(); for(int i = 0; i  3중 for문으로 문제를 풀었는데, 다른 사람 풀이를 보니 재귀호출로 조금 더 가독성 있게 풀 수 있다는 것을 확인하고재귀호출로 구현하였다.  #include #include using namespace std;int GetCombinationCnt(vector Number, int Sum, int Count, int Idx){ int Result = 0; if(Count == 3) { return ..