LeetCode: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.


题目描述的不是很清楚,其实就是第i+1个字符串是第i个字符串的读法,第一字符串为 “1”

比如第四个字符串是1211,它的读法是 1个1、1个2,2个1,因此第五个字符串是111221。

第五个字符串的读法是:3个1、2个2、1个1,因此第六个字符串是312211                  本文地址

......

简单的模拟就可以。

复制代码
 1 class Solution {
 2 public:
 3     string countAndSay(int n) {
 4         if(n < 1)return "";
 5         string prev = "1";
 6         for(int i = 2; i <= n; i++)
 7         {
 8             char curChar = prev[0];
 9             int times = 1;//curChar 出现的次数
10             string tmpstr;
11             prev.push_back('#');//处理边界条件
12             for(int k = 1; k < prev.size(); k++)
13             {
14                 if(prev[k] == curChar)
15                     times++;
16                 else
17                 {
18                     tmpstr += to_string(times);
19                     tmpstr.push_back(curChar);
20                     curChar = prev[k];
21                     times = 1;
22                 }
23             }
24             prev = tmpstr;
25         }
26         return prev;
27     }
28 };
复制代码

其实我们可以发现字符串中永远只会出现1,2,3这三个字符,假设第k个字符串中出现了4,那么第k-1个字符串必定有四个相同的字符连续出现,假设这个字符为1,则第k-1个字符串为x1111y。第k-1个字符串是第k-2个字符串的读法,即第k-2个字符串可以读为“x个1,1个1,1个y” 或者“*个x,1个1,1个1,y个*”,这两种读法分别可以合并成“x+1个1,1个y” 和 “*个x,2个1,y个*”,代表的字符串分别是“(x+1)11y” 和 "x21y",即k-1个字符串为“(x+1)11y” 或 "x21y",不可能为“x1111y”.

 






本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3776356.html,如需转载请自行联系原作者

目录
相关文章
LeetCode 357. Count Numbers with Unique Digits
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。
201 0
LeetCode 357. Count Numbers with Unique Digits
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
194 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 222. Count Complete Tree Nodes
给出一个完全二叉树,求出该树的节点个数。
125 0
LeetCode 222. Count Complete Tree Nodes
|
测试技术
LeetCode 204. Count Primes
统计所有小于非负整数 n 的质数的数量。
143 0
LeetCode 204. Count Primes
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode之Count and Say
LeetCode之Count and Say
132 0
LeetCode 204 Count Primes(质数计数)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50617645 翻译 计算小于一个非负整数n的质数的个数。
898 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
190 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行

热门文章

最新文章