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

 

posted @ 2016-05-25 18:26  牧马人夏峥  阅读(117)  评论(0编辑  收藏  举报