코드카타-숫자 짝꿍

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string X, string Y) {
vector<int> cntX(10, 0), cntY(10, 0);
for (char c : X) {
int d = c - '0';
cntX[d]++;
}
for (char c : Y) {
int d = c - '0';
cntY[d]++;
}
long long total = 0;
for (int d = 0; d <= 9; d++) {
total += min(cntX[d], cntY[d]);
}
string ans;
ans.reserve((size_t)total);
for (int d = 9; d >= 0; d--) {
int common = 0;
common = min(cntX[d], cntY[d]);
ans.append(common, char('0' + d));
}
if (ans.empty()) {
return "-1";
}
if (ans[0] == '0') {
return "0";
}
return ans;
}
분반 수업에서 배운 내용
람다: 이름 없는 즉석 함수.
[capture](parameters) -> return_type {
// body
};
[캡처](매개변수) -> 반환형{
본문
};
의 형식인데, “한 번만(또는 짧게) 쓸 로직”을 굳이 함수 이름을 만들지 않고, 필요한 자리에서 바로 정의해서 전달할 때 많이 쓴다.
채팅 숫자 야구 프로그램 진행도
실습 환경 구성을 하고 있는 중이다.
즉, 1-4강을 듣고 실습하고 있다.
'TIL' 카테고리의 다른 글
| 3/11일자 TIL (0) | 2026.03.11 |
|---|---|
| 3/10일자 TIL (0) | 2026.03.10 |
| 3/06일자 TIL (0) | 2026.03.06 |
| 3/05일자 TIL 및 KPT회고 (0) | 2026.03.05 |
| 3/04일자 TIL (0) | 2026.03.04 |