zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Leetcode 之Count and Say(35)

LeetCode and count 35
2023-09-14 08:57:33 时间

很有意思的一道题,不好想啊。

string getNext(string &s)
      {
          char start = s[0];
          int count = 0;
          stringstream ss;
          for (int i = 0; i < s.size(); i++)
          {
              if (start == s[i])
              {
                  count++;
              }
              else
              {
                  ss << count << start;
                  count = 1;
                  start = s[i];
              }
          }
          ss << count << start;

          return ss.str();
      }

      string countAndSay(int n)
      {
          string s = "1";
          for (int i = 0; i < n; i++)
              s = getNext(s);
      }
View Code