leetCode 38. Count and Say 字符串

38. 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.

根据规律可以写出后面的:

 1.     1 2.     11 3.     21 4.     1211 5.     111221  6.     312211 7.     13112221 8.     1113213211 9.     31131211131221 10.   13211311123113112211

思路:

1.用2个串来替换存储记录result,tmp。

2.记录当前值n,获取当前值最大连续长度m。

3.tmp串追加"m",再追加"n"。

代码如下:

class Solution {
public:
    string countAndSay(int n) {
    	if (--n < 0)
    		return "";
    	string result = "1";
    	string tmp;  //临时串
    	int step = 1;//步长
    	char cur;    //当前元素
    	while (n)
    	{
    		cur = result[0];
    		for (int i = 0; i < result.size(); i++)
    		{
    			if ( i+1 < result.size() && result[i] == result[i + 1])
    			{
    				step++;
    			}
    			else
    			{
    				stringstream stepStream;
    				stepStream << step;
    				string stepStr = stepStream.str();
    				tmp.append(1,stepStr[0]);
    				tmp.append(1,cur);
    				step = 1;
    				cur = result[i + 1];
    			}
    		}
    		swap(result, tmp);
    		tmp = "";
    		n--;
    	}
    	return result;
    }
};

2016-08-10 17:24:26

时间: 2024-09-29 18:02:10

leetCode 38. Count and Say 字符串的相关文章

LeetCode 38 Count and Say(字符串规律输出)

题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111221—>312211—>…. 按照上面的规律进行求解出第n个字符串是什么. 规律:相连的数字有多少个然后添加上这个数字 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 按照规律进行求解字符串 */ public class So

leetCode 38.Count and Say (计数和发言) 解题思路和方法

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" o

5.Leetcode 38:Count and Say 笔记

1:题目描述 Given an integer n, generate the nth term of the count-and-say sequence. 找规律,给定一个数n后,写出第n行的字符串 2:题目分析 突然发现自己的Say实在不发达,看了半个多小时才知道咋回事.n就是再说n-1行的内容:"有'x1'个'num1'和'x2'个'num2'--",分析完毕,开始写代码 3:解题思路 1 class Solution(object): 2 def countAndSay(se

Java [leetcode 38]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.

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: 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 121

[LeetCode 38] Count and Say

题目链接: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 on

[LeetCode] 38. 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

19.2.3 [LeetCode 38] 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

LeetCode:Count and Say

1.题目名称 Count and Say(按照数字重复出现计数并生成字符串) 2.题目地址 https://leetcode.com/problems/count-and-say/ 3.题目内容 英文:The count-and-say sequence is the sequence of integers beginning as follows 中文:给出正整数n,返回"count-and-say"序列的第n项 说明: count-and-say序列形如:1, 11, 21, 1