外观数列-力扣LeetCode 38题C++版

方法一:递归 : 双指针维护相同数字的左右边界 c++版

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

        string s = countAndSay(n-1), ans;
        int left = 0, right = 0; // 双指针维护相同数字的左右边界

        while (left < s.size()) {
            while (right < s.size() && s[right] == s[left]) right++;
            ans += to_string(right-left) + s[left];
            left = right;
        }
        return ans;
    }
};

你可能感兴趣的:(LeetCode,leetcode,c++,算法)