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的更多相关文章

  1. 【一天一道LeetCode】#38. Count and Say

    一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...

  2. 【LeetCode】38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  3. 【LeetCode算法-38】Count and Say

    LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...

  4. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  5. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

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

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)

    目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...

  9. 【LeetCode题解】2_两数相加

    目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...

随机推荐

  1. MongoDB基础一篇就够了

    MongoDB linux安装MongoDB Windows安装MongoDB 查看当前数据库名称 db 查看所有数据库名称 列出所有在物理上存在的数据库 show dbs 切换数据库 如果数据库不存 ...

  2. Keil相关问题

    1.keil重选则器件 2. 移植FREERTOS出错 .\Objects\RTOSDemo.axf: Error: L6406E: No space in execution regions wit ...

  3. IDEA下运行 mybatis报错 Parameter 'arg0' not found. Available parameters are [autoRecharge, id, param1, param2]

    电脑换系统之后重新安装一了 一下idea 项目运行时出现了以下错误, [autoRecharge, id, param1, param2] 或 [arg0, id, arg1, param2] 参考地 ...

  4. 【JavaScript】 使用Async 和 Promise 完美解决回调地狱

    很久以前就学习过Async和Promise,但总是一知半解的. 今天在写NodeJS的时候,发现好多第三方库使用回调,这样在实际操作中会出现多重回调,这就是传说中的JS回调地狱. 举个例子 有一个方法 ...

  5. Django----列表分页(使用Django的分页组件)

    目的:是为了实现列表分页 1.定制URL http://127.0.0.1:8000/blog/get_article?page=3之前定制URL是在url后增加了/id,这次使用参数的方式 def ...

  6. 高性能HTTP加速器Varnish-3.0.3搭建、配置及优化步骤

    经过一天的努力,终于将Varnish缓存服务器部署到线上服务器了.趁着热乎劲儿,赶紧给大家分享一下.Varnish是一个轻量级的Cache和反向代理软件.先进的设计理念和成熟的设计框架是Varnish ...

  7. (八)shell工具-重点

    8.1 cut cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的.cut 命令从文件的每一行剪切字节.字符和字段并将这些字节.字符和字段输出. 1.基本用法 cut [选项参数]  fil ...

  8. Alpha冲刺(1/10)——2019.4.23

    作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 ...

  9. linux系统,关于Python多版本共存

    http://www.cnblogs.com/Yiutto/p/5962906.html 给个地址直接看八~

  10. 记录新项目中遇到的技术及自己忘记的技术点【DES加密解密,MD5加密,字符串压缩、解压,字符串截取等操作】

    一.DES加密.解密 #region DES加密解密 /// <summary> /// 进行DES加密 /// </summary> /// <param name=& ...