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.
My Thought
理解题目理解了半天。。。
好吧,大致意思是,第一个数字是“1”(字符串的形式)。从第二个数字开始,字符串按照前一个字符串的读法决定。比如,前一个是“1”,那么就是1个“1”,于是第二个字符串是“11”,依次类推。
一个递归解决问题。
伪代码:
PROCEDURE countAndSay(n)
if n = 1
return "1"
else
// 获取前一个字符串
preString = countAndSay(n-1)
// 按规则读字符串,作为返回串、
return read(preString)
Code(C++ 0ms)
class Solution {
public:
string countAndSay(int n) {
if(n==1)
return "1";
string pre = countAndSay(n-1);
// read rule
pre+='#';
string ret="",temp="";
int num = 1;
char ch=pre[0];
for(int i=1;i<pre.size();++i){
if(pre[i]!=ch){
temp+=('0'+num);
temp+=ch;
ret.append(temp);
temp="";
ch=pre[i];
num=0;
}
num+=1;
}
return ret;
}
};
最后0ms ACC 镇啊,激动(虽然很ez23333
题解在我的github上会第一时间更新,有兴趣的可以star一下
LeetCode题解38.Count and Say的更多相关文章
- 【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
- 【LeetCode】38 - 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
LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- [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
1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...
- 【LeetCode题解】2_两数相加
目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...
随机推荐
- pads layout 自动打地孔
对于PCBLayout来说,后期处理也是一项费时操作,比如为了让板子耦合的更好,会在板子空旷位置打上很多地过孔.“自动打地孔”则会让你省时又省心,一不小心就提前了工期哦,哈哈! 一.下面打开一个简单的 ...
- FreeRTOS 启动进程调度后,程序卡死的部分原因分析。
现象:1,RTOS 使用时 系统卡启动文件 B .处. 原因分析:该种情况是由于定义开启了中断,但是未开启中断处理服务.程序执行到中断响应式无对应的程序响应 ...
- 基于cxf的app文件上传接口(带回显功能)
1.SaleImpl @Override public String uploadPic(final List<Attachment> attachments) { return this ...
- TPshop之邮箱注册配置教程--附加常见问题集合
准备:企业邮箱(开启POP/SMTP功能) 一.步骤教程: 1.登录企业邮箱(QQ邮箱示例) QQ邮箱 POP3:pop.qq.com SMTP:smtp.qq.com SMTP端口号:25 邮箱 ...
- Snakes 的 Naïve Graph
题解: 首先分析一下这个问题 发现等价于是求n之内与n互素的数的个数,即欧拉函数 这个可以线性筛 但发现还应该减去$x^2==1$的情况 这个东西不是那么好处理 考虑用中国剩余定理拆 因为$p1^{a ...
- Web程序-----批量生成二维码并形成一张图片
需求场景:客户根据前台界面列表所选择的数据,根据需要的信息批量生成二维码并形成一张图片,并且每张图片显示的二维码数量是固定的,需要分页(即总共生成的二维码图片超出每页显示的需另起一页生成),并下载到客 ...
- read()和write()
1.write() 函数定义:ssize_t write (int fd, const void * buf, size_t count); 函数说明:write()会把参数buf所指的内存写入cou ...
- selenium使用遇到的问题(selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.)
1.安装pip3 install selenium 2.使用browser=webdriver.Chrome()时报错 :selenium.common.exceptions.WebDriverExc ...
- VSTO 基础随笔
1.往组合框添加条目 With Me .ComboBox1.Items .Add( "Ports" ) .Add( "Igor" ) End With 2.文本 ...
- BZOJ.4160.[NEERC2009]Exclusive Access 2(状压DP Dilworth定理)
BZOJ DAG中,根据\(Dilworth\)定理,有 \(最长反链=最小链覆盖\),也有 \(最长链=最小反链划分数-1\)(这个是指最短的最长链?并不是很确定=-=),即把所有点划分成最少的集合 ...