[LeetCode] 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"
这道计数和读法问题还是第一次遇到,看似挺复杂,其实仔细一看,算法很简单,就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的 string 里。代码如下:
- class Solution {
- public:
- string countAndSay(int n) {
- if (n <= ) return "";
- string res = "";
- while (--n) {
- string cur = "";
- for (int i = ; i < res.size(); ++i) {
- int cnt = ;
- while (i + < res.size() && res[i] == res[i + ]) {
- ++cnt;
- ++i;
- }
- cur += to_string(cnt) + res[i];
- }
- res = cur;
- }
- return res;
- }
- };
博主出于好奇打印出了前 12 个数字,发现一个很有意思的现象,不管打印到后面多少位,出现的数字只是由 1, 2 和3 组成,网上也有人发现了并分析了原因,参见这个帖子,前十二个数字如下:
Github 同步地址:
https://github.com/grandyang/leetcode/issues/38
类似题目:
参考资料:
https://leetcode.com/problems/count-and-say/
https://leetcode.com/problems/count-and-say/discuss/16000/Show-an-Answer-in-Java
https://leetcode.com/problems/count-and-say/discuss/16043/C%2B%2B-solution-easy-understand
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Count and Say 计数和读法的更多相关文章
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LintCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- [Leetcode] count and say 计数和说
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode]Count and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
随机推荐
- scikit-learn一般实例之四:使用管道和GridSearchCV选择降维
本例构建一个管道来进行降维和预测的工作:先降维,接着通过支持向量分类器进行预测.本例将演示与在网格搜索过程进行单变量特征选择相比,怎样使用GrideSearchCV和管道来优化单一的CV跑无监督的PC ...
- .NET 实现并行的几种方式(四)
本随笔续接:.NET 实现并行的几种方式(三) 八.await.async - 异步方法的秘密武器 1) 使用async修饰符 和 await运算符 轻易实现异步方法 前三篇随笔已经介绍了多种方式.利 ...
- 在docker容器中vi指令找不到
在使用docker容器时,有时候里边没有安装vi,敲vi命令时提示说:vi: command not found,这个时候就需要安装vi,可是当你敲apt-get install vi命令时,提示: ...
- python之最强王者(11)——异常(exception)
1.Python 异常处理 python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 异常处理: 本站Python教程会具体介绍. ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- [deviceone开发]-do_Album的简单示例
一.简介do_Album用来打开手机系统提供的相册,能选择一张或多张图片返回给开发者,通常相册的图片比较大,要经过缩放.有的时候用户也需要把别的地方获取到到图片收藏到系统相册.这个示例简单展示这个组件 ...
- Struts2的经典入门
一:Struts2的起源与背景 在了解Struts2之前我们先来聊聊Struts1,我们都知道在很长的一段时间内,所有的MVC框架中,Struts1他是处于一个超级大咖的地位,无论是从市场角度和使用的 ...
- Android开发aidl使用中linkToDeath和unlinkToDeath的使用
1.Binder死亡代理 这一节首先将介绍Binder类中比较重要的两个方法linkToDeath和unlinkToDeath.我们知道Binder是运行在服务进程,若服务端进程因为某种原因“ ...
- iOS开发系列--无限循环的图片浏览器
--UIKit之UIScrollView 概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件 ...
- Android教程收集贴
Loader & REST Rest Loader Tutorial [github源码] [源码演示] [github作者主页] Twitter Timeline Sample for An ...