## 一.題目描述
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.
## 二.題目分析
題目的內容很多,其實就是根據一個數的讀法,組合出下一個數,比如11,讀作2(個)1,因此下個數是21;同理,21讀作1(個)2、1(個)1,因此下個數是1211…
根據題意,發現該題沒有什么高級的技巧,代碼中主要使用了`string`和`stringstream`,需要加入頭文件`#include <string>`,`#include <sstream>`,關于`string`和`stringstream`的用法,可參考:[http://blog.csdn.net/xw20084898/article/details/21939811](http://blog.csdn.net/xw20084898/article/details/21939811)?。該程序只是實現了對題目要求的模擬,然后輸出結果。
## 三.示例代碼
~~~
#include <string>
#include <sstream>
using namespace std;
class Solution
{
public:
string CountAndSay(int n)
{
string result = "1";
while (--n)
result = theNextStr(result);
return result;
}
private:
string theNextStr(const string& str)
{
if (str.empty())
return string();
stringstream result;
int strSize = str.size();
int count = 1; // 計數
for (int Index = 0; Index < strSize - 1; Index++)
{
if (str[Index] == str[Index + 1])
count++;
else
{
result << count << str[Index];
count = 1;
}
}
result << count << str[strSize - 1]; // 最后一位
return result.str();
}
};
~~~

## 四.小結
需要好好學習一下`string`和`stringstream`的用法。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching