按照说明模拟。。。

stringstream挺好用的

class Solution {
public:
    string count(const string& now) {
        stringstream ss;
        int i = 0;
        int size = now.size();
        int prev = -1;
        while(i < size) {
           while(i + 1< size && now[i] == now[i + 1]) i++;
           ss << (i - prev) << now[i];
           prev = i;
           i++;
        }
        return ss.str();
    }
    string countAndSay(int n) {
        string now = "1";
        for(int i = 1 ; i < n  ; i++) {
            now = count(now);
        }
        return now;
    }
};

 

相关文章:

  • 2022-01-04
  • 2021-09-09
  • 2021-10-10
  • 2022-02-14
  • 2021-07-07
  • 2021-09-07
猜你喜欢
  • 2021-11-14
  • 2021-10-02
  • 2021-07-29
  • 2022-02-05
  • 2021-09-10
  • 2021-08-15
  • 2021-08-11
相关资源
相似解决方案