[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"
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] 38. Count and Say 计数和读法的更多相关文章
- [LeetCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- [LeetCode]Count and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- [LintCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- [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] 38. Count and Say_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
随机推荐
- LeetCode 26:删除排序数组中的重复项 Remove Duplicates from Sorted Array
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. Give ...
- python在字节流中对int24的转换
python在字节流中对int24的转换 概述 最近在写项目的过程中,需要对从串口中读取的数据进行处理,本来用C写完了,但是却一直拿不到正确的数据包,可能是因为自己太菜了.后来用了python重新写了 ...
- poj-3404 Bridge over a rough river Ad Hoc
Bridge over a rough river POJ - 3404 Bridge over a rough river Time Limit: 1000MS Memory Limit: 65 ...
- 【Python】itertools之product函数
[转载]源博客 product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in ...
- C#,WPF,DataGrid,Excel,导出
private void btnExport_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Stopwatch sw = n ...
- AJAX小示例
一. 基本内容 定义:AJAX(Asynchronous Javascript And XML)翻译成中文就是"异步的Javascript和XML",即使用Javascript语言 ...
- vue学习指南:第四篇(详细) - vue的 :class 和 :style
1. :class = “a” 说明 vue 中有个叫 a 的属性 这个标签的class 就是 a的值 2. :class = “{ active:isactive }” Active的存在取决于 i ...
- 简单使用:SpringBoot使用freemarker
使用步骤: a : 添加依赖 b: 创建模板文件 保存位置resources/templates 目录下 文件后缀名.ftl c 编写controller 把结果传递给模板 在resources.te ...
- DV型、OV型、EV型证书的主要区别
DV型和OV型证书的区别 DV和OV型证书最大的差别是:DV型证书不包含企业名称信息:而OV型证书包含企业名称信息,以下是两者差别对比表: DV OV 包含企业名称信息 否 是 验证公司名称 ...
- vue 开发系列(十) VUE 作用域插槽
使用场景 官方解释,有时让插槽内容能够访问子组件中才有的数据是很有用的.比如我们在使用ant-design-vue 的表格控件时. <a-table-column title="注释& ...