# Count and Say

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 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, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

Solution:

class Solution {
public:
string countAndSay(int n) {
if(n == 0) return "";
string current = "1";
string next = "";
stringstream stream;
while(--n)
{
int count = 1;
current += '-';
for(int i = 1; i < current.length(); ++i)
{
if(current[i] == current[i-1]) count++;
else
{
string tmp;
stream << count;
stream >> tmp;
next += tmp + current[i-1];
stream.clear();
count = 1;
}
}
current = next;
next.clear();
}

return current;
}
};

• 本文已收录于以下专栏：

## LeetCode Count and say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11122...

## 【leetcode c++】38 Count and Say

Count and Say The count-and-say sequence is the sequenceof integers beginning as follows: 1, 11, 2...
• hqq39
• 2015-06-26 22:40
• 309

## Leetcode - 38. Count and Say

38. Count and Say题目简介 The count-and-say sequence is the sequence of integers beginning as follows:...

## leetcode 题解 || Count and Say 问题

problem： The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 2...

## LeetCode算法题之Count and Say

//问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 121...

## <leetcode系列> Count And Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)