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. 2018开源中国最受欢迎的中国软件MyBatis-Plus

    2018开源中国最受欢迎的中国软件MyBatis-Plus 官方网址:https://mp.baomidou.com 中国软件,中文文档 什么是MyBatis-Plus? 进入官方第一句话:为简化开发 ...

  2. Python3学习笔记(urllib模块的使用)

    转载地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None,  ...

  3. webpack 学习小结

    webpack 是一个模块打包工具(前提要安装 node使用npm来安装webpack) 1.安装webpack,webpack-cli , webpack-dev-server //全局安装 npm ...

  4. 题解 P5315 【头像上传】

    本题就是按照题目模拟, 只是要注意一些细节问题. 看代码注释 #include<bits/stdc++.h> using namespace std; int n,l,g,i; int m ...

  5. L1-046 整除光棍 大数除法

    L1-046 整除光棍(20 分) 这里所谓的"光棍",并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整 ...

  6. ssh-copy-id使用非默认22端口时

    http://blog.sina.com.cn/s/blog_541a3cf10101epzf.html

  7. 解决使用redis作为session缓存 报错 Error: no such key 的问题

    spring的issue https://github.com/spring-projects/spring-session/issues/954 原答案是 Updated my codes to 2 ...

  8. Hadoop优化

    一.影响MR程序效率的因素 1.计算机性能: CPU.内存.磁盘.网络, 计算机的性能会影响MR程序的速度与效率 2.I/O方面 1)数据倾斜(代码优化) 2)map和reduce数量设置不合理(通过 ...

  9. iOS报错:linker command failed with exit code 1 (use -v to see invocation) 问题解决方式之一

    百度库原版本:3.2.1  更新为:4.2.0,两个库相隔2年时间: 问题i: 更新CocoaPods的同时更新了百度地图库的版本,运行程序报错: linker command failed with ...

  10. scrapy递归解析和post请求

    递归解析 递归爬取解析多页页面数据 每一个页面对应一个url,则scrapy工程需要对每一个页码对应的url依次发起请求,然后通过对应的解析方法进行作者和段子内容的解析. 实现方案: 1.将每一个页码 ...