# 38. Count and Say

**<font color=red>难度:Easy<font>**

## 刷题内容
> 原题连接

* https://leetcode.com/problems/count-and-say/

> 内容描述

```
The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.


Example 1:

Input: 1
Output: "1"
Example 2:

Input: 4
Output: "1211"
```

> 思路
******- 时间复杂度: O(n)******- 空间复杂度: O(1)******

首先要弄清楚题目的意思n == 1时就是1，接着念一个1就是 one 1，n == 2时就是“11”，接着念2个1就是 two 1，n == 3时就是“21”。再念1个2，1个1就是 one 2 one 1，n == 4时就是“1211”，接着依次类推，我们写个循环模拟上面的操作即可。

```cpp
class Solution {
public:
    string countAndSay(int n) {
        string str;
        str.push_back('0' + 1);
        n--;
        while(n)
        {
            string temp;
            for(int i = 0;i < str.length();)
            {
                int j = 0;
                while(i + j < str.length() && str[i] == str[j + i])
                    ++j;
                temp.push_back('0' + j);
                temp.push_back(str[i]);
                i += j;
            }
            --n;
            str = temp;
        }
        return str;
    }
};
```