Leetcode 之Count and Say(35)

Posted 牧马人夏峥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 之Count and Say(35)相关的知识,希望对你有一定的参考价值。

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

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

 

以上是关于Leetcode 之Count and Say(35)的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-38. Count and Say

5.Leetcode 38:Count and Say 笔记

LeetCode 38. 外观数列 Count and Say

LeetCode 38. 外观数列 Count and Say

LeetCode 37. Count and Say

LeetCode38. Count and Say