当前位置:网站首页>38. appearance series

38. appearance series

2022-06-12 05:25:00 anieoo

Original link :38. Look-and-say sequence

 

solution:
        Double pointer

① Default string from state "1" Start , Since the state has been defined "1", Just loop n-1 Secondary steps ②
② Given a string , Enumerate each character j,k from j Start , Find the characters j The continuous interval of [j,k - 1], Number and character j Joining together to t After the string ,j Continue from k Position start enumeration
③ Every time ② Update after operation res

class Solution {
public:
    string countAndSay(int n) {
        string res = "1";   // Define the first item in the sequence 
        for(int i = 0;i < n - 1;i++) {
            string t;
            for(int j = 0;j < res.size();) {
                int k = j + 1;
                while(k < res.size() && res[k] == res[j]) k++;
                t += to_string(k - j) + res[j];
                j = k;
            }
            res = t;
        }
        return res;
    }
};

原网站

版权声明
本文为[anieoo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120524058008.html