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. (详细)华为P8 GRA-UL00的Usb调试模式在哪里开启的方法

    经常我们使用Pc通过数据线连接上安卓手机的时候,如果手机没有开启usb开发者调试模式,Pc则没能够成功检测到我们的手机,有时候我们使用的一些功能较强的应用软件好比之前我们使用的一个应用软件引号精灵,老 ...

  2. linux统配符

    linux通配符注意:linux的通配符和三剑客的表达式是不一样的,因为,代表的意义是有较大区别的.通配符一般用户命令行bash环境,而linux正则表达式用于grep,sed,awk场景. *    ...

  3. [原创]Vivado SDK Initializing s/w repositories不动

    21:56:16 INFO : Launching XSDB server: xsdb.bat C:/Xilinx/SDK/2015.4/scripts/xsdb/xsdb/xsdb-server.t ...

  4. [转] Ramda 函数库参考教程

    学习函数式编程的过程中,我接触到了 Ramda.js. 我发现,这是一个很重要的库,提供了许多有用的方法,每个 JavaScript 程序员都应该掌握这个工具. 你可能会问,Underscore 和  ...

  5. eclipse乱码解决

    设置utf-8 1.点击window>preferences>content types 2.点击右侧Text 3.点击Java Source File 4.下面输入UTF-8 5.点击u ...

  6. 绕最新版安全狗-附上sqlmap的tamper

    在t00ls 里面我发的,放博客分享 记录一下 小菜写文章,太菜 希望大佬放过我!  主要分享一下,绕狗的思路环境:windows7 + phpstudy + safedog v4.0  + Mysq ...

  7. python: 列表的方法

    操作 函数 使用方法 备注 索引 index in: example.index(‘creative’) --- 1 in:example[1,] --- [’creative’, [’京东’,996 ...

  8. Mac下使用数据库将Excel数据转换存入.plist

    记录于2013/10/26   基本步骤: 1.将Excel表格另存为.csv格式 2.用类似TextWrangler工具将.csv文件转成UTF-8格式 3.使用火狐插件SQLite Manager ...

  9. APIO2018 被屠记

    占坑 day0 10:40才起床 感觉一点也不好 下午去了趟80中拿牌子然而没有到,白浪费我颓废时间. day0.5 早上第一课讲二分凸优化,有点瞌睡 第二课讲匹配相关,感觉这篇文章涵盖了大部分内容 ...

  10. 心得体会,搞清楚你为什么学习C++?

    小编作为一名初学者时,从来没问过自己学习C语言.C++等语言是为了什么? 一开始,接触到这个行业可以说是有种魔力引导我,感到了很大的兴趣,很有意思. 我试着读资料,报名学习,找资料,可算是功夫不负有心 ...