코테준비 - Count and Say

정상화·2023년 2월 26일

LeetCode

목록 보기
36/222

Count and Say

class Solution {
public:
    string countAndSay(int n) {
        if (n == 1) return "1";

        string prev = countAndSay(n - 1);
        string result = "";
        int cnt = 1;
        for (auto it = prev.begin(); it != prev.end(); it++) {
            if (it + 1 != prev.end() && (*it) == *(it + 1)) {
                cnt++;
            } else {
                result += (char) (cnt + '0');
                result += (*it);
                cnt = 1;
            }
        }
        return result;
    }
};
profile
백엔드 희망

0개의 댓글